Chain of Responsibility Pattern in Java (Example)
The Chain of Responsibility pattern explained: pass a request along a chain of handlers until one handles it, decoupling sender from receiver. Servlet filters, approval workflows, Java code, UML, when to use.
🟠Design Patterns · Behavioral · Senior
The Chain of Responsibility pattern passes a request along a chain of handlers until one of them handles it. Think of an expense approval: a small amount is approved by your manager, a bigger one goes up to the director, a huge one to the VP. The requester doesn't need to know who'll approve it — they just submit, and the request flows up the chain. It's how you decouple a sender from a whole set of possible handlers.
The problem
An expense request needs approval, and who approves depends on the amount. A naive version is a big conditional: if (amt < 1000) manager; else if (amt < 10000) director; else vp;. The requester is coupled to every approval level, and changing the rules (add a new level, shift a threshold) means editing that conditional. You want each approver to just decide "can I handle this? If not, pass it on."
The solution: a chain of handlers
🟠Each handler holds a reference to the next handler. It either handles the request or delegates to the next:
abstract class Approver {
protected Approver next;
public Approver setNext(Approver n){ this.next = n; return n; }
public void handle(Expense e) {
if (canApprove(e)) approve(e);
else if (next != null) next.handle(e); // pass it along
else System.out.println("No one could approve");
}
protected abstract boolean canApprove(Expense e);
protected abstract void approve(Expense e);
}
class Manager extends Approver { boolean canApprove(Expense e){ return e.amt < 1000; } /*…*/ }
class Director extends Approver { boolean canApprove(Expense e){ return e.amt < 10000; } /*…*/ }
class VP extends Approver { boolean canApprove(Expense e){ return true; } /*…*/ }
// build the chain:
Approver chain = new Manager();
chain.setNext(new Director()).setNext(new VP());
chain.handle(new Expense(5000)); // Manager passes → Director approves
The request walks the chain until a handler takes it. The sender knows only the first handler, not who ultimately handles it. Adding a level or reordering is just changing how the chain is linked — no edits to the handlers or the sender.
Where you meet it
🟠It's everywhere in real systems: servlet filters / middleware (each filter handles or passes the request — auth, logging, compression), logging frameworks (a log record passes handlers by level), event bubbling in UIs (an event propagates up the component tree), exception handling (each catch/handler up the stack gets a chance), and approval workflows. Any "pipeline of handlers where each may handle or forward" is this pattern.
Key design choices
🔴 Two nuances worth naming: (1) a request might reach the end unhandled — decide whether that's an error or a silent no-op. (2) Some chains stop at the first handler that acts (approval); others let every handler process it as it passes (a filter pipeline where each does its bit). Both are valid variants of the same structure — clarifying which you mean shows understanding.
When to use it
Use Chain of Responsibility when more than one object may handle a request and the handler isn't known in advance, when you want to decouple sender from receiver, or when the set/order of handlers should be configurable at runtime. If exactly one object always handles the request, a direct call is simpler.
The interview-ready summary
"Chain of Responsibility links handlers so a request passes down the chain until one handles it — each handler either processes it or forwards to the next. The sender only knows the first handler, so it's decoupled from who actually handles it, and I can add, remove, or reorder handlers without touching the sender. Real examples: servlet filters / middleware, logging by level, UI event bubbling, and approval workflows. I'd decide whether an unhandled request is an error, and whether the chain stops at the first handler or every handler acts." That's the pattern with its real-world forms."
What to read next
- All Design Patterns (hub) — you've now covered all 16
- Command Pattern — another way to structure requests
- Design a Parking Lot — patterns applied in a full LLD problem
- ← The complete System Design guide (hub)