Favor Composition Over Inheritance part 1 | Source: Coding Delight

abstract class BaseCar

{

public Wheel FrontLeft { get; protected set; }

public Wheel FrontRight { get; protected set; }

public Wheel RearLeft { get; protected set; }

public Wheel RearRight { get; protected set; }

public abstract string Manufacturer { get; }



public abstract void TurnLeft();

public abstract void TurnRight();

public abstract void Accelerate(double kmsPerHour);

}
abstract class Car : BaseCar

{

protected Car()

{

this.FrontLeft = new Wheel();

this.FrontRight = new Wheel();

this.BackLeft = new Wheel();

this.BackRight = new Wheel();

}



public override void TurnLeft(double degrees)

{

this.FrontLeft.TurnLeft(degrees);

this.FrontRight.TurnLeft(degrees);

}



public override void TurnRight(double degrees)

{

this.FrontRight.TurnRight(degrees);

this.FrontLeft.TurnRight(degrees);

}



public override void Accelerate(double kph)

{

this.FrontLeft.Rotate(kph);

this.FrontRight.Rotate(kph);

}

}
class ToyotaCorolla : Car

{

public override string Manufacturer

{

get { return "Toyota"; }

}

}
class ToyotaCorollaSports : ToyotaCorolla

{

public override void Accelerate(double kph)

{

this.RearLeft.Rotate(kph);

this.RearRight.Rotate(kph);

}

}
abstract class FrontWheelDriveCar : Car

{

public override void Accelerate(double kph)

{

this.FrontLeft.Rotate(kph);

this.FrontRight.Rotate(kph);

}

}
abstract class RearWheelDriveCar : Car

{

public override void Accelerate(double kph)

{

this.RearLeft.Rotate(kph);

this.RearRight.Rotate(kph);

}

}
abstract class AllWheelDriveCar : Car

{

public override void Accelerate(double kph)

{

this.FrontLeft.Rotate(kph);

this.FrontRight.Rotate(kph);

this.RearLeft.Rotate(kph);

this.RearRight.Rotate(kph);

}

}
class MitsubishiTitan : AllWheelDriveCar

{

public override string Manufacturer

{

get { return "Mitsubishi"; }

}

}
class diagram

--

--

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