Chain Of Responsibility PatternIn 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 PatternLet's understand the example of Chain of Responsibility Pattern by the above UML diagram. UML for Chain of Responsibility Pattern:Implementation of above UML:Step 1Create a Logger abstract class. Step 2Create a ConsoleBasedLogger class. File: ConsoleBasedLogger.java Step 3Create a DebugBasedLogger class. File: DebugBasedLogger.java Step 4Create a ErrorBasedLogger class. File: ErrorBasedLogger.java Step 5Create a ChainOfResponsibilityClient class. File: ChainofResponsibilityClient.java OutputNext TopicCommand Design Pattern in Java |