Chain Of Responsibility Pattern
In chain of responsibility, sender sends a request to a chain of objects. The request can be handled by any object in the chain.
A Chain of Responsibility Pattern says that just "avoid coupling the sender of a request to its receiver by giving multiple objects a chance to handle the request". For example, an ATM uses the Chain of Responsibility design pattern in money giving process.
In other words, we can say that normally each receiver contains reference of another receiver. If one object cannot handle the request then it passes the same to the next receiver and so on.
Advantage of Chain of Responsibility Pattern
Usage of Chain of Responsibility Pattern:
It is used:
Example of Chain of Responsibility Pattern
Let's understand the example of Chain of Responsibility Pattern by the above UML diagram.
UML for Chain of Responsibility Pattern:
Implementation of above UML:
Create a Logger abstract class.
Create a ConsoleBasedLogger class.
Create a DebugBasedLogger class.
Create a ErrorBasedLogger class.
Create a ChainOfResponsibilityClient class.