The Gang of Four [1] advocates favoring "composition over inheritance".
An outline of this concept in provided below as a refresher.
Outline
- Favor "object composition" over "class inheritence".
Instead of inheriting from a parent class consider composing references to the parent class with the type or behavior desired.
- The use of inheritence can be seen as a white-box reuse.
The reason being the sub-class commonly has visibility into the parent class.
- Object composition resembles black-box reuse.
Through the use of composition, the implementation details are better encapsulated and enables delegating such behaviors.
UML quick notes
Composition
is a stronger form of aggregation
. A solid-diamond towards the composite class.
Aggregation
is a stronger form of association
. An un-filled diamond towards the composite class.
Reference
[1] Gang of Four 1995:20 [2]