Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
Use Chain of Responsibility when
- more than one object may handle a request, and the handler isn't known a priori. The handler should be ascertained automatically
- you want to issue a request to one of several objects without specifying the receiver explicitly
- the set of objects that can handle a request should be specified dynamically
- Handler (HelpHandler)
- defines an interface for handling requests
- (optional) implements the successor link
- ConcreteHandler (PrintButton, PrintDialog)
- handles requests it is responsible for
- can access its successor
- if the ConcreteHandler can handle the request, it does so; otherwise it forwards the request to its successor
- Client
- initiates a request to a ConcreteHandler object on the chain
Chain of Responsibility is often applied in conjunction with Composite. There, a component's parent can act as its successor.