Chain of Responsibility Pattern
Pass a request along handlers until one accepts it, with an explicit unhandled result.
· Last updated: 2026-11-07 · 10 min read
Overview
Billing questions and technical questions go to different handlers. If the intake screen inspects each question and chooses a handler, adding a handler also adds a condition to the screen. Instead, the screen can pass the question to the first handler; each handler checks whether it can handle the question and either does so or passes it to the next.
Chain of Responsibility lets a handler take a request or pass it on. Each handler decides whether the request is its business; the caller knows only the first handler. This example stops at the first match and returns an unhandled result when nothing matches.
UML
The caller uses the first Handler. A Handler shares a single promise: take the request or pass it on. Each handler can refer to a successor, and BillingHandler and TechHandler recognize their own ticket kinds. The calling code assembles their order.
History
GoF cataloged Chain of Responsibility as a behavioral pattern in Design Patterns in 1994. A chain avoids binding the sender to one particular receiver.1
The direct ancestor of this structure is event and context-help propagation in late-1980s object-oriented GUI frameworks. In the ET++ framework presented by Weinand, Gamma, and Marty at OOPSLA 1988—and in Apple's MacApp—a user command or help request started at the most specific focused view and walked upward through enclosing containers toward the application object until the first handler capable of answering took charge.2 Event bubbling in the browser DOM follows the exact same path from leaf node to root.
Why use it?
Handler order and composition change without touching the intake screen. Only the chain-linking code changes; each handler keeps its own accept-or-forward decision.
The forwarding rule alone sets this apart from a pipeline that must run every step. The chain stops at the first taker; with no taker, the request runs to the end. Rather than drop it quietly at that end, define an unhandled result and decide who tells the user about it.
Example
Chain the technical handler behind the billing handler, then send one billing question.
using System;
using System.Collections.Generic;
using System.Linq;
Handler chain = new BillingHandler(new TechHandler(null));
Reply result = chain.Handle(new Ticket("billing"));
Console.WriteLine(result.Handled + ": " + result.Message);
sealed record Ticket(string Kind);
sealed record Reply(bool Handled, string Message);
// Keep acceptance rules in handlers and share the forwarding flow in the base class.
abstract class Handler(Handler? next)
{
public Reply Handle(Ticket ticket)
{
Reply? result = TryHandle(ticket);
if (result is not null) return result;
// null means this handler declines the ticket, so pass it to the next handler.
// If there is no successor, return Handled=false for the whole chain.
return next?.Handle(ticket) ?? new Reply(false, "unhandled: " + ticket.Kind);
}
protected abstract Reply? TryHandle(Ticket ticket);
}
// Decline with null without choosing the next handler here.
sealed class BillingHandler(Handler? next) : Handler(next)
{
protected override Reply? TryHandle(Ticket ticket) =>
ticket.Kind == "billing" ? new Reply(true, "billing desk") : null;
}
sealed class TechHandler(Handler? next) : Handler(next)
{
protected override Reply? TryHandle(Ticket ticket) =>
ticket.Kind == "technical" ? new Reply(true, "technical desk") : null;
}
True: billing deskBillingHandler accepts it, so the request does not reach TechHandler.
An extended example
This time, send a technical ticket and a ticket that no handler supports. The second comes back as Handled=false instead of pretending to succeed. An unhandled ticket is still a normal result.
A technical ticket first reaches BillingHandler, which does not handle it. Its TryHandle returns null, so the shared Handle method passes the same ticket to TechHandler. That handler recognizes the kind, creates the technical-desk reply, and returns it through the chain to the caller.
Neither handler accepts an other ticket. When the final TechHandler also returns null, there is no next handler, so Handle creates a reply with Handled=false. A handler's null asks the chain to continue; the caller's false reports that the entire chain found no handler.
using System;
using System.Collections.Generic;
using System.Linq;
Handler chain = new BillingHandler(new TechHandler(null));
foreach (string kind in new[] { "technical", "other" })
{
Reply result = chain.Handle(new Ticket(kind));
Console.WriteLine(result.Handled + ": " + result.Message);
}
sealed record Ticket(string Kind);
sealed record Reply(bool Handled, string Message);
// Keep acceptance rules in handlers and share the forwarding flow in the base class.
abstract class Handler(Handler? next)
{
public Reply Handle(Ticket ticket)
{
Reply? result = TryHandle(ticket);
if (result is not null) return result;
// null means this handler declines the ticket, so pass it to the next handler.
// If there is no successor, return Handled=false for the whole chain.
return next?.Handle(ticket) ?? new Reply(false, "unhandled: " + ticket.Kind);
}
protected abstract Reply? TryHandle(Ticket ticket);
}
// Decline with null without choosing the next handler here.
sealed class BillingHandler(Handler? next) : Handler(next)
{
protected override Reply? TryHandle(Ticket ticket) =>
ticket.Kind == "billing" ? new Reply(true, "billing desk") : null;
}
sealed class TechHandler(Handler? next) : Handler(next)
{
protected override Reply? TryHandle(Ticket ticket) =>
ticket.Kind == "technical" ? new Reply(true, "technical desk") : null;
}
True: technical desk
False: unhandled: otherReorder the handlers and a request that several of them could accept lands at a different one. The chain here stops after two steps, and chains assembled at runtime also need protection against cycles. A link that points back to an earlier handler leaves the request circling forever.
TypeScript example
Intake code does nothing but hand the request to the first handler, and each handler decides whether the request is its responsibility. No conditionals on ticket kind sit in the intake code. Detaching handler selection from the intake screen makes the chain's order and composition easy to change.
type Ticket = Readonly<{ kind: string }>;
type Reply = Readonly<{ handled: boolean; message: string }>;
// Keep acceptance rules in handlers and share the forwarding flow in the base class.
abstract class Handler {
constructor(private readonly next: Handler | null) {}
handle(ticket: Ticket): Reply {
const reply = this.tryHandle(ticket);
if (reply !== null) return reply;
return this.next?.handle(ticket) ?? { handled: false, message: "unhandled: " + ticket.kind };
}
protected abstract tryHandle(ticket: Ticket): Reply | null;
}
// Decline with null without choosing the next handler here.
class BillingHandler extends Handler {
protected override tryHandle(ticket: Ticket): Reply | null {
return ticket.kind === "billing" ? { handled: true, message: "billing desk" } : null;
}
}
class TechHandler extends Handler {
protected override tryHandle(ticket: Ticket): Reply | null {
return ticket.kind === "technical" ? { handled: true, message: "technical desk" } : null;
}
}
const chain = new BillingHandler(new TechHandler(null));
for (const kind of ["billing", "technical", "other"]) {
const result = chain.handle({ kind });
console.log(result.handled + ": " + result.message);
}true: billing desk
true: technical desk
false: unhandled: othernull means the current handler did not take the request. handled=false is the final result at the end of the chain, and this example stops at the first handler that takes it. The two answer at different levels, so keep them distinct.
Boundary with middleware pipelines and practical pitfalls
Production codebases often conflate classic GoF Chain of Responsibility with a web framework's middleware pipeline (Intercepting Filter), so it helps to separate request routing from the composition of extra work. The GoF example in this article searches for one handler to take ownership and finish handling the request. Web middleware also chooses whether to pass control onward or end the response. In ASP.NET Core, Use() can wrap request and response work around an awaited next delegate. Express next() passes control to the next middleware; work that follows response completion can use the response finish event. Composing logging, authentication, and compression in this way also invites comparison with a Decorator Pattern stack.34
The classic failure mode in a hand-rolled chain is the silent drop: a request walks past every handler and vanishes without a trace. While GoF leaves unhandled requests open by design, domain workflows should either pin a catch-all fallback handler at the tail of the chain or return an explicit Handled = false (or Result Type) so the caller can never mistake "nobody answered" for success.
Watch the call stack as well. When each link invokes next.Handle() recursively, stack depth grows linearly with chain length and cyclic wiring blows the stack immediately. If chains are assembled dynamically from configuration, driving a flat list of handlers from an outer loop or Command Pattern dispatcher keeps stack frames shallow and makes cycle detection trivial.
Related reading
Footnotes
- Gamma, Helm, Johnson, Vlissides. Design Patterns (1994). Publisher record. Behavioral patterns, Chain of Responsibility. ↩
- André Weinand, Erich Gamma, and Rudolf Marty, "ET++ — An Object-Oriented Application Framework in C++," OOPSLA '88 Proceedings, ACM, 1988, pp. 46–57. DOI. ↩
- Microsoft Learn, "ASP.NET Core Middleware." Official documentation. ↩
- Express, "Using middleware." Ending a response, passing control with next(), and skipped later handlers. Official guide. ↩