Chain of Responsibility
Pass a request along a chain of handlers — each handler decides to process it or pass it to the next, decoupling sender from receiver.
Why this matters
When a single function handles every case with a growing chain of if/elif blocks, it becomes a maintenance trap. The Chain of Responsibility pattern lets you build a pipeline of handler objects — each independently responsible for what it knows. When a request arrives, it travels down the chain until a handler claims it. Adding a new handler means creating one new class, never touching existing code.
Where you already see it
HTTP middleware stacks (Express, Django middleware) are chains of responsibility — each middleware handles what it owns (auth, logging, rate-limiting) and passes the request forward. Event bubbling in the DOM is a chain. Exception handling in many languages walks up a handler chain. The pattern is everywhere in frameworks once you look for it.
💡Key takeaway
Chain of Responsibility shines when you have multiple potential handlers and don't know in advance which one will process the request — build the chain, then let the request find its handler.
🔧 Some exercises may still have errors. If something seems wrong, use the Feedback button (bottom-right of the page) to report it — it helps us fix it fast.
Hint: Chain of Responsibility shines when you have multiple potential handlers and don't know in advance which one will process the request.
✗ Your version