Chain of responsibility
medium 45 minA request travels past several handlers until one takes it, or until one stops it.
Watch lesson4:36
Chapter mnemonic
Behavioural design patterns
Curious cats investigate missing milk. One spots squirrels towing vans.
Remember it
The few things worth knowing cold, and how to make them stick.
Memory hook
Take it, or pass it on. And know where the sand goes.
Sieves on a mountain: each knows the size of its holes. The order sorts the stones, and a tub at the foot catches the rest. In an office it is the help desk: level 1, level 2, manager, director.
Cheat sheet
- Handler
- takes or passes
- Base handler
- next, default passing on
- Client
- knows the first
- Dispatch
- one takes it
- Filter
- first refusal stops
- Pipeline
- before, next, after
- End
- catch-all or exception
- Live change
- swap the whole list
- Broken link
- closed for security
- One key?
- use a map
Spot it when you see
- escalates to
- up to this amount
- before every request
- checks in a row
- the first that matches
- bubbles up
Tricks that stick
- CHAIN: Chain the handlers, Handle or pass, Avoid coupling, Independent rules, Next if you cannot.
- Reading an order: ask of each neighbour pair, what if they were swapped?
- Say first and last: first is what the sender holds, last is what catches the sand.
Do not forget
- Keeping the last handler instead of the first
- No decision for the end
- A wide handler in front
- Forgotten return
- Request data in a shared handler
- Relinking under load