Chain of Responsibility
- is a behavioral design pattern that lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain
Code Structure
/behavioral-design-patterns/chain-of-responsibility-(cor)-pattern/chain-of-responsibility-structure.png)
Code Example
Click here to expand...
interface Handler { void setNext(Handler handler); void handle(request); }abstract class BaseHandler implements Handler { private Handler next; void setNext(Handler next) { this.next = next; } void handle(request); }class ConcreteHandler1 extends BaseHandler { void handle(request) { if (canHandleRequest1(request)) { // handle logic } else { next.handle(request); } } } class ConcreteHandler2 extends BaseHandler { void handle(request) { if (canHandleRequest2(request)) { // handle logic } else { next.handle(request); } } } ...main() { Handler h1 = new ConcreteHandler1(); Handler h2 = new ConcreteHandler1(); Handler h3 = new ConcreteHandler1(); h1.setNext(h2); h2.setNext(h3); // ... h1.handle(request); }
Comparisons
Click here to expand...
Link to originalChain of Responsibility, Command, Mediator, and Observer address various ways of connecting senders and receivers of requests:
- Chain of Responsibility passes a request sequentially along a dynamic chain of potential receivers until one of them handles it
- Command establishes unidirectional connections between senders and receivers
- Mediator eliminates direct connections between senders and receivers, forcing them to communicate indirectly via a mediator object
- Observer lets receivers dynamically subscribe to and unsubscribe from receiving requests
Link to originalChain of Responsibility Pattern vs Command Pattern
- Handlers in the Chain of Responsibility Pattern can be implemented as Commands. In this case, you can execute a lot of different handler-command-operations over the same request-context object
- Requests in the Chain of Responsibility Pattern can be implemented as Commands. In this case, you can execute the same request-command-operation through a series of different handler-contexts
Link to originalChain of Responsibility Pattern and Decorator Pattern have very similar class structures. Both patterns rely on recursive composition to pass the execution through a series of objects. However, there are several crucial differences.
- The CoR handlers can execute arbitrary operations independently of each other. They can also stop passing the request further at any point. On the other hand, various Decorators can extend the object’s behavior while keeping it consistent with the base interface. In addition, decorators aren’t allowed to break the flow of the request.
Link to originalChain of Responsibility Pattern vs Object Tree Pattern
- Chain of Responsibility is often used in conjunction with Composite. In this case, when a leaf component gets a request, it may pass it through the chain of all of the parent components down to the root of the object tree