Skip to main content
Design Patterns 80 XP · 8 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.

Showing
Ad (728×90)

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.

The problem

Bad

The solution

Good

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.

Done with this lesson?

Mark it complete to earn XP and track your progress.