Chain of responsibility
Description
A request travels along a chain of handlers, each of which decides whether to take the request or pass it to the next handler. The sender doesn’t know who will ultimately handle the request; the handlers know only their immediate successor. The chain naturally orders handlers from most-specific (front) to most-generic (back), with a default or graceful-degradation behavior at the end. The defining property is the handler-acceptance decision: each handler asks “is this mine?” and either claims the request or defers. Compared to plain multi-hop routing, where every hop is a routing decision but the destination is implied by routing metadata, chain-of-responsibility makes the handler’s willingness the routing criterion: the request finds its handler by trial-pass rather than by addressing. The shape generalizes far beyond OOP. Customer support escalation, DOM event bubbling, HTTP middleware (an even more general decorator-chain), exception handlers, bug-triage queues, the legal escalation through courts of higher jurisdiction, and the editorial review chain at a publication all share this structure.Triggers
User-initiated: User describes wanting “try X first, then Y, then Z” or escalation semantics. Vocabulary cues: “escalate,” “fall through,” “bubble up,” “try in order,” “first match wins,” “if not handled, then.” Agent-initiated: Agent notices nested if/else dispatching where a sequence of conditions is checked and the first match wins, especially when the conditions evolve over time. Candidate inference: “lift these into handler objects on a chain; new conditions become new handlers without disturbing the existing chain.” Situation-shape signals: Tiered processing hierarchies. Specificity gradients (most-specific first, falling back to general). Sender that shouldn’t need to know which handler will respond. Plugins / extensions that contribute conditional handling.Exclusions
- Handler selection is cheap and deterministic — if you know which handler the request belongs to from a simple lookup, dispatching through a chain is wasted work; direct dispatch is clearer and faster.
- Order doesn’t matter — chain-of-responsibility is intrinsically ordered. If handlers should fire in parallel or in arbitrary order, observer / pub-sub is the right primitive.
- Fall-through silently loses requests — if the chain reaches its end with no handler accepting and the request is silently dropped, you have a hidden failure mode. Always either have an explicit default at the chain’s tail or loudly fail on no-handler.
- The chain becomes a god-class — when every handler “might handle” every request, the chain becomes a giant series of speculative checks. Specialization criteria need to be clear and disjoint enough that each request’s traversal is short.
Structure
Relationships
- multi-hop-routing — chain-of-responsibility is the handler-acceptance specialization of multi-hop-routing; both have the each-hop-knows-only-next property, but chain decides on handler willingness while multi-hop routes by destination metadata.
- graceful-degradation — chain-of-responsibility is one of the canonical mechanisms for graceful degradation: specific handlers at the front; broader fallbacks deeper; ultimate default at the end.
- cost-cascade — cost-cascade is naturally a chain: cheap handler first; expensive handler downstream; the chain’s deferral semantics give the cascade its shape.
- decorator — middleware chains are simultaneously decorator (each layer adds behavior) and chain-of-responsibility (each layer can short-circuit); the same primitive viewed from different intent lenses.
- doctrine — escalation doctrines (tier 1 handles X; tier 2 handles Y; otherwise escalate) are chain-of-responsibility encoded in the doctrine’s named rules.
Examples
Web-framework middleware stacks (Ruby Rack; Node.js Express's `req, res, next` pipeline) — commonly characterized as a Chain of Responsibility blended with the Decorator pattern. · computer-science
Web-framework middleware stacks (Ruby Rack; Node.js Express's `req, res, next` pipeline) — commonly characterized as a Chain of Responsibility blended with the Decorator pattern. · computer-science
next(); Rack’s call to the next app in the stack). The sender (the framework dispatching the request) does not know which handler will ultimately answer it, and each handler does not know what follows it — exactly the decoupling the pattern names. A middleware can also short-circuit the chain: an auth layer that returns 401 stops the request before it reaches the route handler, the classic Chain-of-Responsibility move of one handler accepting and terminating.It is usually described as a blend with the Decorator pattern, and the blend is structurally honest. Pure Chain of Responsibility passes a request along until exactly one handler consumes it and the traversal ends. Middleware departs from that in a telling way: most handlers do their work (logging, body-parsing, session setup) and then keep passing the request along, wrapping the eventual route handler in added behavior rather than competing to be the sole responder. So the chain provides the pass-and-short-circuit topology while the decorator provides the each-layer-adds-behavior semantics. Recognizing the chain part is what tells you the order of middleware is load-bearing and that any layer can quietly intercept the request before it reaches where you think it goes.Customer-support tiered escalation · business
Customer-support tiered escalation · business
Bug-triage queues · computer-science
Bug-triage queues · computer-science
Customer-support escalation hierarchies (tier 1 → tier 2 → tier 3) — predates and outlives the software pattern · business
Customer-support escalation hierarchies (tier 1 → tier 2 → tier 3) — predates and outlives the software pattern · business
DOM event bubbling · computer-science
DOM event bubbling · computer-science
click events bubble from target node up through ancestors until a handler calls stopPropagation or the document is reached.Editorial review at a publication · journalism-media-studies-and-communication
Editorial review at a publication · journalism-media-studies-and-communication
Exception handling · computer-science
Exception handling · computer-science
try/catch blocks form a chain up the call stack; each level decides handle or rethrow.Gamma, Helm, Johnson, Vlissides (1994), Design Patterns: Elements of Reusable Object-Oriented Software (Gang of Four). Behavioral pattern, ch. 5. · computer-science
Gamma, Helm, Johnson, Vlissides (1994), Design Patterns: Elements of Reusable Object-Oriented Software (Gang of Four). Behavioral pattern, ch. 5. · computer-science
HTTP middleware · computer-science
HTTP middleware · computer-science
Legal appeal hierarchy · law
Legal appeal hierarchy · law
Linux signal handlers · computer-science
Linux signal handlers · computer-science