Favor Composition Over Inheritance Part 2 | Source: Coding Delight

interface IDriving

{

void Accelerate(double kph);

}
interface ISteering

{

void TurnLeft(double degrees);

void TurnRight(double degrees);

}
interface IManufacturer

{

string Name { get; }

}
interface ICar

{

IWheel FrontLeft { get; }

IWheel FrontRight { get; }

IWheel RearLeft { get; }

IWheel RearRight { get; }



ISteering Steering { get; }

IDriving Driving { get; }

IManufacturer Manufacturer { get; }

}
class TwoWheelDrive : IDriving

{

private readonly IWheel left;

private readonly IWheel right;



public TwoWheelDrive(IWheel left, IWheel right)

{

this.left = left;

this.right = right;

}



public void Accelerate(double kph)

{

this.left.RotationSpeed += kph;

this.right.RotationSpeed += kph;

}

}
class FrontSteering : ISteering

{

private readonly IWheel frontLeft;

private readonly IWheel frontRight;



public FrontSteering(IWheel frontLeft, IWheel frontRight)

{

this.frontLeft = frontLeft;

this.frontRight = frontRight;

}



public void TurnLeft(double degrees)

{

this.frontLeft.Angle -= degrees;

this.frontRight.Angle -= degrees;

}



public void TurnRight(double degrees)

{

this.frontLeft.Angle += degrees;

this.frontRight.Angle += degrees;

}

}
class Toyota : IManufacturer

{

private static IManufacturer instance;



private Toyota() { }



public string Name { get { return "Toyota"; } }



public static IManufacturer GetInstance()

{

return instance ?? (instance = new Toyota());

}

}
class ToyotaCorolla : ICar

{

public ToyotaCorolla()

{

this.FrontLeft = new Wheel();

this.FrontRight = new Wheel();

this.RearLeft = new Wheel();

this.RearRight = new Wheel(0, 0);

this.Steering = new FrontSteering(this.FrontLeft, this.FrontRight);

this.Driving = new TwoWheelDrive(this.FrontLeft, this.FrontRight);

this.Manufacturer = Toyota.GetInstance();

}



public IWheel FrontLeft { get; private set; }

public IWheel FrontRight { get; private set; }

public IWheel RearLeft { get; private set; }

public IWheel RearRight { get; private set; }



public ISteering Steering { get; private set; }

public IDriving Driving { get; private set; }

public IManufacturer Manufacturer { get; private set; }

}
class ToyotaCorollaSports : ICar

{

public ToyotaCorollaSports()

{

this.FrontLeft = new Wheel();

this.FrontRight = new Wheel();

this.RearLeft = new Wheel();

this.RearRight = new Wheel(0, 0);

this.Steering = new FrontSteering(this.FrontLeft, this.FrontRight);

this.Driving = new TwoWheelDrive(this.RearLeft, this.RearRight);

this.Manufacturer = Toyota.GetInstance();

}



public IWheel FrontLeft { get; private set; }

public IWheel FrontRight { get; private set; }

public IWheel RearLeft { get; private set; }

public IWheel RearRight { get; private set; }



public ISteering Steering { get; private set; }

public IDriving Driving { get; private set; }

public IManufacturer Manufacturer { get; private set; }

}
class Car : ICar

{

public Car(IWheel frontLeft, IWheel frontRight, IWheel rearLeft,

IWheel rearRight, ISteering steering, IDriving driving,

IManufacturer manufacturer, string carName)

{

this.FrontLeft = frontLeft;

this.FrontRight = frontRight;

this.RearLeft = rearLeft;

this.RearRight = rearRight;

this.Steering = steering;

this.Driving = driving;

this.Manufacturer = manufacturer;

this.Name = carName;

}



public IWheel FrontLeft { get; private set; }

public IWheel FrontRight { get; private set; }

public IWheel RearLeft { get; private set; }

public IWheel RearRight { get; private set; }



public ISteering Steering { get; private set; }

public IDriving Driving { get; private set; }

public IManufacturer Manufacturer { get; private set; }

public string Name { get; private set; }

}
static class CarFactory

{

public static ICar ToyotaCorolla()

{

IWheel frontLeft = new Wheel();

IWheel frontRight = new Wheel();

return new Car(frontLeft, frontRight, new Wheel(), new Wheel(),

new FrontSteering(frontLeft, frontRight),

new TwoWheelDrive(frontLeft, frontRight), Toyota.GetInstance(),

"Corolla");

}

public static ICar ToyotaCorollaSports()

{

IWheel frontLeft = new Wheel();

IWheel frontRight = new Wheel();

IWheel rearLeft = new Wheel();

IWheel rearRight = new Wheel();

return new Car(frontLeft, frontRight, rearLeft, rearRight,

new FrontSteering(frontLeft, frontRight),

new TwoWheelDrive(rearLeft, rearRight), Toyota.GetInstance(),

"Corolla Sports");

}

}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store