Learn

Architecture & pattern guides

Read the idea, build it on a Grid, prove it in a challenge. Short, practical guides — illustrated with RodGrid's own nodes — on system design, design patterns, flow modeling, and how to get the most out of the app.

All articles

Chain of Responsibility

Pass a request along a line of handlers, each of which either deals with it or hands it to the next, so the sender never has to know which one will act, and decide on purpose what happens when none does.

Who signs for a €3,200 laptop?

In a company's expense tool, the answer depends on the amount. A team lead can approve up to €500, a department head up to €5,000, the finance director up to €25,000, and anything larger goes to the CEO. The first version wrote that into ExpenseService.submit(claim) as a ladder of ifs, one per limit.

The ladder worked until the organisation moved. Finance wanted the travel claims a department head would sign to go to the travel desk instead, which knows the travel policy. The Lisbon office has no department heads, so its claims skip that rung. The limits change every year at budget time. Each change meant editing the one method every claim passes through, a method that had to know the whole org chart.

Each approver answers one question

The redesign gives every approver the same small interface, Approver, with one method, handle(claim), and one field, next, a reference to another approver. Each approver asks only whether the claim is within its own authority. TeamLead.handle() decides claims up to €500 and otherwise calls next.handle(claim); DepartmentHead does the same with €5,000. The expense service no longer chooses anyone. It hands the claim to the first approver and waits for the answer.

The Gang of Four called this design Chain of Responsibility. The sender does not know which handler will act, and each handler knows only its successor, not the whole order. The claim travels as a plain data object carrying the amount, the category, the office and the receipts; turning an operation itself into an object is the subject of Command.

A claim enters at the first approver

The expense service knows only the team lead. Each approver implements Approver and holds one reference, to the next.

Follow the laptop through the diagram. The team lead's limit is €500, so the claim moves on. The department head's €5,000 covers it; she approves it, and the chain stops there. The finance director never sees the claim, and the expense service gets its answer without having picked the person who gave it. Handling means taking responsibility, not saying yes: the department head could just as well reject the laptop, and the rejection also ends the trip. The claim's life after the decision, approved and then paid, is a job for State; the chain decides only who signs.

Someone has to assemble the line

Neither the approvers nor the sender decide the order. That job belongs to one piece of assembly code, run at startup or when an office's settings load, which creates the approvers and links each one to its successor. That is where the travel desk goes in. TravelDesk signs travel claims up to €5,000 and passes everything else, and it is inserted by pointing the team lead's next at it and its own next at the department head. No other approver changes. Lisbon gets its own chain from the same classes: team lead, then finance director.

Watch outCommon mistake: having each approver know the whole chain order itself instead of just its successor, for example a list of the roles that come after it so it can skip someone on leave. Inserting the travel desk then means editing every one of those lists, and Lisbon needs a second set. When an approver holds a single next reference, set from outside, inserting a handler means relinking two references and nothing else.

Order is part of the policy. In this chain the first approver willing to act wins, so approvers have to run from the narrowest authority to the widest. Put the finance director first by mistake and she signs every claim up to €25,000, coffee receipts included, while the team leads never see one. Keep the order in that one assembly function, and test the chain it builds with claims just below and just above each limit.

If the approvers differed only by a limit, a sorted table searched for the first limit the amount fits under would be shorter and would show the whole policy in one place. The chain pays off when handlers ask different kinds of questions, as the travel desk does with the category, and when the line is assembled differently per office.

A €60,000 claim falls off the end

A €60,000 server order passes the team lead, the department head and the finance director, whose limit is €25,000, and her next points at nothing. What does submit() hand back to the expense service? The classic pattern does not guarantee that anyone handles a request, so that answer is a decision too, and there are three usual ones:

  • Nothing. The last approver sees there is no successor and returns no decision, so the service gets back an empty result. If the service treats an empty result as pending, the claim sits there with nobody responsible for it. Without even that check, calling next.handle() on a missing reference throws in most languages instead.
  • An error. The service gets back an explicit unhandled result and turns it into something visible: an alert to finance, a message to the employee.
  • A decision. A terminal handler at the end accepts whatever reaches it. Here that is CeoApprover, with no limit, so the service always gets back an approval or a rejection from someone entitled to give it.

For these approvals, where the CEO can sign any amount, the terminal handler is the right end, and the error is the safety net for a chain someone assembled without it. Silence is right only when no taker is a normal outcome, such as a chain of shortcut handlers where a key combination nobody bound should do nothing. There, a last handler that deliberately does nothing is a Null Object, and it spares every handler from checking whether it has a successor.

Middleware is a chain that rarely stops

Middleware stacks in web frameworks are often described as a chain of responsibility, and the linking is the same: each layer receives the request and a way to call the next. The default is the opposite, though. A layer usually does its part, such as logging or loading a session, and calls next, so every layer runs and stopping is the exception, an authentication layer answering 401; in the classic chain, acting is what stops the request. Layers that always run and wrap the one after them are closer to Decorator.

What the end of the line gives back

Open the Chain of Responsibility Challenge and draw this approval line, or a chain of your own, in at most eight nodes: a Port / Interface node for the Approver contract, a Handler node per approver, an Actor for the expense service and a Message node for the claim. The service's only arrow goes to the first handler; if it points at any other, the sender has started choosing again. Then make the end of the chain explicit: draw the last handler, and decide what a claim that passes every limit gets back, a decision or a visible unhandled, never an empty answer.

Practice this