Card 08/ 08
Recap: Chain of Responsibility Pattern
Each handler handles or passes on, and the end of the chain needs an answer too.
Chain of Responsibility sends a request along a line of handlers until one deals with it.
- Intent. Decouple a sender from its receiver by letting several objects in turn have the chance to handle the request.
- Structure. Each handler holds the next; the client sends only to the first.
- Changing the chain. Adding, removing or reordering handlers changes the line that builds the chain, not the sender.
- Order matters. The first handler that accepts a request wins, so put the checks that must win first.
- In the wild. Web framework middleware, where most links act and then call
next(). - The trap. A request no handler accepts falls off the end and returns nothing; end every chain with a default handler or an error.
- When not. One known handler, or every request must be handled and nothing guarantees it.
← → move through sections and cards