コンポジション・オーバー・インヘリタンスは広く引用されている設計原則です — ベースクラスから継承する(インヘリタンス)のではなく、動作を組み合わせる(コンポジション)ことでオブジェクトを構築することを支持しています。インヘリタンスは重大な欠点を持っていますが、コンポジションはそれらを回避します。ただし、両者にはそれぞれの位置があります。
インヘリタンス対コンポジション
INHERITANCE → a class EXTENDS another, inheriting its behavior ("IS-A" relationship):
class Dog extends Animal
COMPOSITION → a class is BUILT FROM other objects/behaviors ("HAS-A" / uses):
class Car { constructor() { this.engine = new Engine(); } } // composes behaviors
→ composition: combine smaller pieces; inheritance: derive from a parent
インヘリタンスの問題点
✗ TIGHT COUPLING → subclasses are tightly bound to their parent's implementation (changes
to the parent can break subclasses — the "fragile base class" problem)
✗ RIGID hierarchies → deep/wide inheritance trees become hard to change and understand
✗ Inflexible → a class can usually only inherit from ONE parent; behavior is fixed at compile
time; hard to combine behaviors from multiple sources
✗ Forces an "IS-A" relationship that may not truly hold; exposes parent internals
