Chain of responsibility
An expense request needs an approver, but the sender should not have to know which one. We pass it down a line of handlers, each of which deals with it or hands it on.
In 30 seconds
Chain of responsibility is a way to get a request to whoever can deal with it, without the sender knowing who that is. The possible receivers, called handlers, are linked in a line.
The sender gives the request to the first handler. Each handler either deals with it or passes it to the next one. Handlers can be added, removed or reordered without touching the sender.
Web server middleware works this way, and so do clicks in a browser, which travel from an element up through the elements that contain it.
Chain of responsibility, step by step
Simplified: in the classic pattern one handler deals with the request and the chain stops. Middleware and browser events are looser variants, where several handlers may act on the same request.
- Step 1: The sender knows too much. An employee needs an expense approved. Who may approve it depends on the amount, so the code that sends the request checks the limits itself and calls the right person directly. The sender is now tied to every approver and every limit. Change one limit or add an approver, and the sender must be rewritten.
- Step 2: Link the handlers. Line the approvers up instead. Each one, called a handler, knows only its own limit and the next handler in line. Every handler follows the same small rule: if the request is within my limit I deal with it, otherwise I hand it to the next. The sender now talks to the first link only.
- Step 3: Handled at the first link. A €60 request enters the chain. The lead’s limit is €100 and 60 is within it, so the lead approves and the request goes no further. The manager and the director never hear about it. A handler that deals with a request ends the trip.
- Step 4: Passed along. Now €450. It is over the lead’s limit, so the lead does nothing with it except pass it on. The manager’s limit is €1,000, so the manager approves. The employee sent this request exactly as before, to the first link, and did not need to know who would end up handling it.
- Step 5: The end of the chain. A €25,000 request is over every limit. With only three links it would drop off the end and get no answer at all, the classic bug of this pattern. The fix is a last handler that catches whatever arrives. Adding it means linking it after the director; the sender and the other handlers are untouched.
- Step 6: Example: middleware. Web servers pass each request through a chain called middleware. Here every handler may do some work and then call next: the logger writes a line and passes the request on. The login check finds no user, answers 401 itself and does not call next, so the handlers after it never run.
- Step 7: Example: event bubbling. A web page is boxes inside boxes, and a click on a button travels up through them: button, then the form around it, then the body. The button has no listener here, so the click moves on; the form’s listener runs. Unlike the approvals, a click keeps going after a listener runs unless that listener calls stopPropagation.
Try it yourself
Learn this first
Learn next
- Event bubbling and capturingA click travels down to its target and back up, calling listeners in order. Beginner
- API gatewaysOne front door for many services: routing, shared checks, and the risk that comes with it. Intermediate
- Observer patternSubscribe, notify, unsubscribe: how one change reaches many listeners. Beginner
Spotted a mistake on this page?