Card 02/ 08
Chain of Responsibility Pattern Structure: The Class and Sequence Diagrams
Each Handler holds the next one, and either handles the request or passes it on.
Chain of Responsibility has three participants: a handler interface, the concrete handlers, and the client that sends a request to the first link. This card reads their roles off the Gang of Four's class diagram, then follows one request down a chain in a sequence diagram to show where it stops.
| Participant | Role |
|---|---|
| Handler | Declares handle, holds a reference to the next handler, and by default passes the request to it. |
| ConcreteHandler | Handles the requests it is responsible for, and passes the rest to the next handler. |
| Client | Builds the chain or receives it, and sends each request to the first handler only. |
The arrow from Handler back to itself is the chain: each handler holds another handler, and the last holds none. The client depends only on the Handler type, so it does not know how many links there are or which of them will answer.
The sequence diagram follows a request that the first two handlers pass on and the third handles. The answer travels back along the same path, through each handle call that is waiting for it.
A request that no handler accepts reaches the last link and finds no next handler. What happens then is a decision the design has to make, and a design that leaves it to chance loses the request without an error.
← → move through sections and cards