Definition
The chain of responsibility is a behavioral design pattern that decouples a sender of a request from its receivers by giving more than one object a chance to handle the request. The request is passed along a chain of potential handlers until one of them processes it.
Core Concepts
| Element | Description |
|---|---|
| Handler | An abstract interface that defines a method for processing requests and optionally for setting the next handler in the chain. |
| Concrete Handler | Implements the handler interface, decides whether to process the request, and if not, forwards it to the next handler. |
| Client | Initiates the request by sending it to the first handler in the chain. |
| Chain | The linked sequence of handler objects, typically assembled at runtime. |
Typical Structure (UML)
Handler(abstract class/interface) with methodhandleRequest(Request)and referencenextHandler.ConcreteHandlerA,ConcreteHandlerB, … extendHandler.Clientcreates handler objects, links them (e.g.,handlerA.setNext(handlerB)), and invokeshandlerA.handleRequest(req).
Historical Context
- First described in the Gang of Four (GoF) design patterns book, Design Patterns: Elements of Reusable Object‑Oriented Software (1994) by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.
- The pattern draws on earlier concepts from object‑oriented programming and responsibility distribution in the Smalltalk community.
Common Use Cases
| Domain | Example |
|---|---|
| GUI frameworks | Event propagation (e.g., mouse clicks) from a component up through container hierarchy until a component consumes the event. |
| Logging | Log messages passed through handlers that filter by severity, writing to console, file, or remote server as appropriate. |
| Middleware | HTTP request processing pipelines where each middleware component can handle, modify, or pass the request onward. |
| Command processing | Business rules applied sequentially; the first rule that validates a command executes the associated action. |
Advantages
- Loose coupling – Sender does not need to know which object will handle the request.
- Flexibility – Handlers can be added, removed, or reordered without altering client code.
- Single Responsibility Principle – Each concrete handler focuses on a specific type of request.
Disadvantages
- Potential for unhandled requests – If no handler processes the request, the system may fail silently unless a default handler is provided.
- Runtime overhead – Traversing the chain can introduce latency, especially with long chains.
- Debugging difficulty – Tracing the path a request takes through multiple handlers can be non‑trivial.
Variants and Related Patterns
| Pattern | Relationship |
|---|---|
| Command | Often combined with chain of responsibility to encapsulate requests as command objects. |
| Interceptor | Similar to chain of responsibility but focuses on pre‑ and post‑processing around a core operation. |
| Decorator | Adds responsibilities to objects dynamically; unlike chain of responsibility, it does not forward requests along a chain. |
| Mediator | Centralizes communication between objects, opposite of the decentralized forwarding of the chain pattern. |
Implementation Example (Java)
// Handler interface
public abstract class Handler {
protected Handler next;
public void setNext(Handler next) { this.next = next; }
public abstract void handle(Request request);
}
// Concrete handler
public class AuthenticationHandler extends Handler {
@Override
public void handle(Request request) {
if (request.isAuthenticated()) {
if (next != null) next.handle(request);
} else {
System.out.println("Authentication failed.");
}
}
}
// Client code
public class Demo {
public static void main(String[] args) {
Handler auth = new AuthenticationHandler();
Handler logger = new LoggingHandler();
auth.setNext(logger);
Request req = new Request(...);
auth.handle(req);
}
}
Criticism and Considerations
- Critics note that overusing the pattern can lead to “spaghetti” chains where the flow of control becomes opaque.
- Modern languages with functional constructs (e.g., Promises, async/await pipelines) sometimes provide alternative mechanisms for request propagation that may be more concise.
See Also
- Command pattern
- Interceptor pattern
- Mediator pattern
- Event bubbling (in web development)
References
- Gamma, E.; Helm, R.; Johnson, R.; Vlissides, J. (1994). Design Patterns: Elements of Reusable Object‑Oriented Software. Addison‑Wesley.
- Fowler, M. (2002). Patterns of Enterprise Application Architecture. Addison‑Wesley.
- Johnson, R. (2004). “Design Patterns and the Chain of Responsibility”. IEEE Software, 21(5).
- “Chain of Responsibility Pattern”. Microsoft Docs. Accessed 2026.
The information presented reflects established knowledge from reputable software engineering literature and documentation.