Behavioral Patterns
Part of the unified study index: Home · canonical comparisons in selection guide.
How objects talk, share work, and evolve over time. All behavioral pages follow the same template: intent, a concrete scenario, structure diagram, trade-offs, usage guidance, runnable examples, an exercise, and a self-check.
Members
- Strategy — swap one step of an algorithm at runtime by delegating to a chosen variant.
- Chain of Responsibility — pass a request along handlers until one claims it.
- Command — package requests as objects so they can be queued, logged, and undone.
- Interpreter — evaluate tiny languages with composable expression objects.
- Iterator — traverse a collection without exposing its storage.
- Mediator — route peer messages through one hub instead of a mesh.
- Memento — snapshot private state for later restore without exposing it.
- Observer — broadcast one subject’s changes to many subscribers.
- State — change behavior with internal status by delegating to the active state.
- Template Method — fix an algorithm’s skeleton, let subclasses vary steps.
- Visitor — add operations over stable element types without editing them.
Confusable trio
| Question | Strategy | State | Template Method |
|---|---|---|---|
| Who picks the active variant? | The caller, at wiring time | The object itself, as status evolves | Nobody at runtime — the subclass fixed at build time |
| How does it change? | Swap the delegated object | Transition between state objects | Override the primitive step |
| Varies what? | One whole step implementation | Behavior per status plus transitions | One step inside a fixed skeleton |
| Use the shop example | Checkout picks card vs cash | Player moves stopped → playing → paused | CSV vs PDF share fetch/save, differ in format |
Rule of thumb: caller picks → Strategy; status evolves → State; skeleton fixed with one open step → Template Method.