Skip to content

Behavioural Patterns — Notes#

Quick chooser#

Goal Pattern
Swap algorithm at runtime Strategy
Many parts react to a change Observer
Different behaviour per state, same interface State
Undo / queue / log / replay a request Command
Pipeline of optional handlers Chain of Responsibility
Fixed algorithm shape, varying steps Template Method
Walk a custom collection Iterator
Decouple noisy n-to-n peers Mediator
New operations over a stable hierarchy Visitor
Save / restore snapshots Memento

Strategy vs Template Method#

  • Strategy: composition (the algorithm is an object you swap).
  • Template Method: inheritance (the algorithm shape lives on the base class).

Observer vs Pub/Sub#

  • Observer: in-process, often synchronous, observers know the subject.
  • Pub/Sub: a broker decouples publishers and subscribers across processes / hosts.

State vs strategy#

Same class shape; intent differs. State = the object's mode dictates which Strategy is active; transitions are part of the model.

Refs#

  • GoF chapters on behavioural patterns.
  • Head First Design Patterns — beginner-friendly.
  • Refactoring (Fowler) — pragmatic application.