Composition Over Inheritance¶
Object composition provides more flexible code reuse than class inheritance, because composition decisions can be made at runtime and don't create rigid hierarchies.
Tension Addressed¶
Inheritance vs. Composition
Recognizable As¶
Delegation to contained objects rather than inherited methods; strategy objects injected at construction; decorator chains; mixins implemented via composition
Canonical Sources¶
Gamma et al., "Design Patterns" (1994); Bloch, "Effective Java" (2001)
Transfers To¶
Functional Programming (function composition), React/UI (component composition), Microservices (service composition)
Inheritance creates three problems: the fragile base class problem (parent changes break children), tight coupling (children depend on parent implementation), and hierarchy rigidity (single inheritance forces unnatural taxonomies). Composition addresses all three.
Sub-concepts: - Delegation: Forwarding requests to composed objects - Strategy pattern essence: Encapsulating algorithms in swappable objects - Decorator pattern essence: Adding behavior by wrapping objects
Related Concepts¶
- Coupling - Software Architecture