design patterns
Chain of Responsibility
The Chain of Responsibility pattern avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. It chains the receiving objects and passes the request along the chain until an object handles it. The key participants are the Handler (defines an interface for handling requests and optionally implements the successor link), ConcreteHandler (handles requests it is responsible for; otherwise forwards to its successor), and Client (initiates the request to a handler in the chain). Think of customer support escalation: your issue first goes to a chatbot, then a junior agent, then a senior agent, then a manager — each level either handles it or passes it up. Use Chain of Responsibility when more than one object may handle a request and the handler is not known in advance, or when you want to issue a request to one of several objects without specifying the receiver explicitly.
Use Cases
- •Middleware pipelines in web frameworks (Express, Koa)
- •Event handling and bubbling in UI systems
- •Logging with different severity level handlers
- •Approval workflows with escalation tiers