Created
February 9, 2016 21:00
-
-
Save tscbp/46d3ac6f3c6c4bb4671a to your computer and use it in GitHub Desktop.
C# implementation of composition > inheritance "MurderRobotDog"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace MurderRobotDog | |
{ | |
interface IBarker | |
{ | |
void Bark(); | |
} | |
class FriendlyBarker : IBarker | |
{ | |
private readonly string _name; | |
public FriendlyBarker( string name ) | |
{ | |
_name = name; | |
} | |
public void Bark() | |
{ | |
Console.WriteLine( $"Woof, I am {_name}" ); | |
} | |
} | |
interface IBarkerFactory | |
{ | |
IBarker GetBarker( string name ); | |
} | |
class FriendlyBarkerFactory : IBarkerFactory | |
{ | |
public IBarker GetBarker( string name ) | |
{ | |
return new FriendlyBarker( name ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MurderRobotDog | |
{ | |
interface IDriver | |
{ | |
void Drive(); | |
} | |
class ConstantSpeedDriver : IDriver | |
{ | |
private DriverInfoModel _info; | |
public ConstantSpeedDriver( DriverInfoModel info ) | |
{ | |
_info = info; | |
} | |
public void Drive() | |
{ | |
_info.Position = _info.Position + _info.Speed; | |
} | |
} | |
interface IDriverFactory | |
{ | |
IDriver GetDriver( DriverInfoModel info ); | |
} | |
class ConstantSpeedDriverFactory : IDriverFactory | |
{ | |
public IDriver GetDriver( DriverInfoModel info ) | |
{ | |
return new ConstantSpeedDriver( info ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MurderRobotDog | |
{ | |
public class DriverInfoModel | |
{ | |
public double Position | |
{ | |
get; | |
set; | |
} | |
public double Speed | |
{ | |
get; | |
set; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MurderRobotDog | |
{ | |
interface IMurderer | |
{ | |
void Murder(); | |
} | |
class Murderer : IMurderer | |
{ | |
public void Murder() | |
{ | |
//Do murderous things | |
} | |
} | |
interface IMurdererFactory | |
{ | |
IMurderer GetMurderer(); | |
} | |
class DefaultMurdererFactory : IMurdererFactory | |
{ | |
public IMurderer GetMurderer() | |
{ | |
return new Murderer(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MurderRobotDog | |
{ | |
class MurderRobotDog : IBarker, IDriver, IMurderer | |
{ | |
private readonly IBarker _barker; | |
private readonly IDriver _driver; | |
private readonly IMurderer _murderer; | |
public MurderRobotDog( IBarkerFactory barkerFactory, IDriverFactory driverFactory, IMurdererFactory murdererFactory, string name ) | |
{ | |
_barker = barkerFactory.GetBarker( name ); | |
_driver = driverFactory.GetDriver( new DriverInfoModel { Position = 0, Speed = 100 } ); | |
_murderer = murdererFactory.GetMurderer(); | |
} | |
public void Murder() | |
{ | |
_murderer.Murder(); | |
} | |
public void Bark() | |
{ | |
_barker.Bark(); | |
} | |
public void Drive() | |
{ | |
_driver.Drive(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MurderRobotDog | |
{ | |
class Program | |
{ | |
static void Main( string[] args ) | |
{ | |
var mrd = new MurderRobotDog( new FriendlyBarkerFactory(), | |
new ConstantSpeedDriverFactory(), | |
new DefaultMurdererFactory(), "Rufus" ); | |
mrd.Bark(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment