In this article, we’ll explain and demonstrate the Chain of Responsibility pattern.
The Chain of Responsibility is a behavioral design pattern that processes a request through a series of processor (handlers/receivers) objects. The request is sent from one handler object to another and processed by one (pure implementation) or all of the handlers. All the handlers are part of the chain.
Real examples
Two simple examples containing a chain logic are:
- A man using an ATM to get cash, (enter pin, amount, receipt);
- a help desk call (options list, press dial buttons, follow the steps).
Participants
The pattern in the short version includes:
- The Handler: defines an interface for handling requests. It can be an abstract class, which optionally implements default methods and the way to set a successor in the chain.
- Many concrete handler objects: process the request and optionally provide access to successors;
CoR can also include:
- a Client object to perform the request and set up the chain;
- a Request object;
- a Response object;
- other design patterns.
The CoR design pattern is not one that is used often, but its core logic makes it useful in several cases.
Continue reading %Introduction to Chain of Responsibility%
by Nicola Pietroluongo via SitePoint
No comments:
Post a Comment