Skip to main content

Sign in to CleanKata

Track your progress, earn XP, and unlock every lesson.

By signing in you agree to our Terms of Use and Privacy Policy.

Design Patterns80 XP8 min

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

Chain of Responsibility β€” CleanKata β€” CleanKata