Handbooks / OOP & Design Patterns / Chapter 8
Behavioral Patterns
7 pages · ~5 min✓ Reviewed
Builds on Structural Patterns. Next up: Code Smells and Refactoring to Patterns.
Behavioral Patterns: The Mental Model
1. What They Are
- Govern how objects communicate and share responsibility
- Move variation (algorithm, reaction, request) out of the caller, behind an interface
| Pattern | Intent |
|---|---|
| Strategy | Swap an algorithm |
| Observer | Notify dependents of a change |
| Command | Turn a request into an object |
| Iterator | Traverse without exposing structure |
| State | Behavior changes with internal state |
| Template Method | Fixed skeleton, subclass fills steps |
| Chain of Responsibility | Pass request along handlers until one handles it |
2. Inheritance or Composition
| Template Method | The other six | |
|---|---|---|
| Reuse via | Inheritance | Composition |
| Bound | Compile-time, subclass | Runtime, swappable object |
| Patterns | Template Method |
Strategy, State and Template Method
1. Strategy and State
- Strategy: client picks one algorithm object behind one interface
- Context delegates; strategies never know each other
- e.g. sort comparator, payment method, compression
- State: context holds a current state and delegates
- States replace the context's state themselves
- e.g. TCP Open/Closed, Order Pending/Shipped
| Strategy | State | |
|---|---|---|
| Class diagram | identical |
Observer, Command and Mediator-Free Messaging
1. Observer
- Subject keeps a list of observers
subscribe/unsubscribe/notify()on change- One-to-many, loosely coupled
- DOM
addEventListener, Reduxsubscribe
javascript
const off = store.subscribe(render) // later off()
subscribe returns the unsubscribe function
- Forgotten unsubscribe: lapsed listener, memory leak
- Notification order is not guaranteed
- Cascading updates can loop
2. Command
Iterator and Chain of Responsibility
1. Iterator
- Iterator = sequential access, structure stays hidden
next/hasNext, Python__iter__/__next__, JSSymbol.iterator, generators- One cursor per iterator, so independent traversals
- Modify while iterating: Java
ConcurrentModificationException, or skipped items - Exhausted iterator cannot be reused: ask for a new one
2. Chain of Responsibility
- Each handler processes the request or passes it on
Choosing the Right Pattern
1. Decision Rules
| Question | Pattern |
|---|---|
| Swappable algorithm? | Strategy |
| Behavior follows lifecycle status? | State |
| Fixed steps, variable parts? | Template Method |
| Many parties react to a change? | Observer |
| Undo, queue or log of actions? | Command |
| Unknown handler picks it up? | Chain of Responsibility |
Behavioral Patterns Cheat Sheet
1. The Seven at a Glance
| Pattern | Core idea | Example |
|---|---|---|
| Strategy | swap algorithm at runtime; client-chosen; composition | sort comparator |
| State | behavior follows internal state; objects drive transitions | order lifecycle |
| Template Method | base-class skeleton; subclasses fill steps; inheritance | framework lifecycle hooks |
| Observer | one-to-many notify | event listeners |
Check yourself
Locked.press() sets door.state = Unlocked() once the code is right. Strategy or State?
- State: the state object swaps itself on the context.
- In Strategy the client picks the object and it stays fixed.
A widget subscribes to a store when it opens and never unsubscribes. It is then closed. What goes wrong?
- The store still holds the widget, so it is never freed and still gets notified.
- This is the lapsed listener leak; unsubscribe on close.
What does this print?
[1, 2, 3]then[].- The first call exhausts the iterator; call
iter()again for a fresh cursor.