Foundations · 5 min

Classification: Creational · Structural · Behavioral

22 patterns grouped by purpose — the fastest way to look one up when you hit a problem.

Creational — who gets to call `new`

About how objects come into existence. Use these when your code is rigid because it names a concrete class directly.

  • Factory Method / Abstract Factory: let somewhere else decide which class to create.
  • Builder: construct an object in many steps, with many options, that needs validation.
  • Prototype: copy yourself instead of building from scratch.
  • Singleton: exactly one instance — and in JS you usually already have it, because ES modules are cached.

Structural — what plugs into what

About how classes/modules connect. Use these when you have to work with legacy code, external APIs, or a system that keeps growing.

  • Adapter: talk to the old thing in your own language.
  • Bridge: separate two axes of change so they vary independently.
  • Composite: treat a tree like a single leaf.
  • Decorator: wrap on extra responsibilities while keeping the same contract.
  • Facade: shrink a 12-function subsystem down to 1 easy-to-understand function.
  • Flyweight: share common data to save memory.
  • Proxy: stand in the middle to control access (lazy, cache, permissions, reactivity).

Behavioral — who does what, who tells whom

About responsibilities and passing messages. The biggest group, because most business-logic bugs live here.

  • Chain of Responsibility: pass work along a chain of handlers, whoever takes it takes it.
  • Command: wrap "a thing to do" as an object so you can queue, undo, retry it.
  • Iterator: traverse without exposing the structure.
  • Mediator: one coordinator instead of a tangled mesh.
  • Memento: snapshots to go back in time.
  • Observer: whoever cares subscribes to listen.
  • State: the state decides the behavior and handles its own transitions.
  • Strategy: pick an algorithm at runtime.
  • Template Method: a fixed skeleton, with a few steps you're allowed to swap.
  • Visitor: add new operations to a closed structure.

Look up by symptom

  • An if/else tree with 4+ branches that only differ in the formula → Strategy (or State if it transitions itself).
  • A constructor with 8 parameters, half of them optional → Builder (or an options object + defaults).
  • Your domain knows the API's field names → Adapter.
  • Component A calls 4 components B, C, D, E directly → Mediator or Observer.
  • Can't test because the logic lives inside a method that calls the API → extract a Strategy and inject it.
  • Users need undo, and you're cloning the whole pile of state by hand → Memento.