Chain of responsabilities
The pattern chains the receiving objects together, and then passes any request messages from object to object until it reaches an object capable of handling the message. The number and type of handler objects isn’t known a priori, they can be configured dynamically. The chaining mechanism uses recursive composition to allow an unlimited number of handlers to be linked.
Chain of Responsibility simplifies object interconnections. Instead of senders and receivers maintaining references to all candidate receivers, each sender keeps a single reference to the head of the chain, and each receiver keeps a single reference to its immediate successor in the chain.
- Reduced coupling
- Added flexibility in assigning responsibilities to objects
- Allows a set of classes to behave as a whole, because events producedin one class can be sent on to other handler classes within the composite
When to use:
- More than one object can handle a request, and the handler isn’t known.
- 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.
Code samples can be downloaded here.