Composition over Inheritance
Advice to build behavior by combining objects instead of deep inheritance chains.
What is Composition over Inheritance?
Composition over inheritance is the advice to build behavior by combining small objects rather than stacking up deep class hierarchies. It keeps code flexible because parts can be swapped.
// Instead of FlyingSwimmingDuck : SwimmingDuck : Duck ...
public class Duck
{
private readonly IFlyBehavior fly;
private readonly ISwimBehavior swim;
public Duck(IFlyBehavior fly, ISwimBehavior swim) { this.fly = fly; this.swim = swim; }
}
Level: Journeyman · More OOP terms at this level
Updated