| layout | pattern | |||
|---|---|---|---|---|
| title | Chain of responsibility | |||
| folder | chain | |||
| permalink | /patterns/chain/ | |||
| categories | Behavioral | |||
| tags |
|
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.
Real world example
The Orc King gives loud orders to his army. The closest one to react is the commander, then officer and then soldier. The commander, officer and soldier here form a chain of responsibility.
In plain words
It helps building a chain of objects. Request enters from one end and keeps going from object to object till it finds the suitable handler.
Wikipedia says
In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain.
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