Composition vs inheritance: a product catalogue

Build objects from small, swappable parts with a factory function instead of a tree of subclasses.

Level Beginner

What you will see

  1. Many kinds of products
  2. A family tree of products
  3. One new idea, twice the classes
  4. A product made of parts
  5. Asking the parts
  6. When inheritance is the right choice

Before / after

Base: composition-over-inheritance-base-jsView the diff

Exercises