- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Chain of Responsibipty Pattern
As the name suggests, the chain of responsibipty pattern creates a chain of receiver objects for a request. This pattern decouples sender and receiver of a request based on type of request. This pattern comes under behavioral patterns.
In this pattern, normally each receiver contains reference to another receiver. If one object cannot handle the request then it passes the same to the next receiver and so on.
Implementation
We have created an abstract class AbstractLogger with a level of logging. Then we have created three types of loggers extending the AbstractLogger. Each logger checks the level of message to its level and print accordingly otherwise does not print and pass the message to its next logger.
Step 1
Create an abstract logger class.
AbstractLogger.java
pubpc abstract class AbstractLogger { pubpc static int INFO = 1; pubpc static int DEBUG = 2; pubpc static int ERROR = 3; protected int level; //next element in chain or responsibipty protected AbstractLogger nextLogger; pubpc void setNextLogger(AbstractLogger nextLogger){ this.nextLogger = nextLogger; } pubpc void logMessage(int level, String message){ if(this.level <= level){ write(message); } if(nextLogger !=null){ nextLogger.logMessage(level, message); } } abstract protected void write(String message); }
Step 2
Create concrete classes extending the logger.
ConsoleLogger.java
pubpc class ConsoleLogger extends AbstractLogger { pubpc ConsoleLogger(int level){ this.level = level; } @Override protected void write(String message) { System.out.println("Standard Console::Logger: " + message); } }
ErrorLogger.java
pubpc class ErrorLogger extends AbstractLogger { pubpc ErrorLogger(int level){ this.level = level; } @Override protected void write(String message) { System.out.println("Error Console::Logger: " + message); } }
FileLogger.java
pubpc class FileLogger extends AbstractLogger { pubpc FileLogger(int level){ this.level = level; } @Override protected void write(String message) { System.out.println("File::Logger: " + message); } }
Step 3
Create different types of loggers. Assign them error levels and set next logger in each logger. Next logger in each logger represents the part of the chain.
ChainPatternDemo.java
pubpc class ChainPatternDemo { private static AbstractLogger getChainOfLoggers(){ AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR); AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG); AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO); errorLogger.setNextLogger(fileLogger); fileLogger.setNextLogger(consoleLogger); return errorLogger; } pubpc static void main(String[] args) { AbstractLogger loggerChain = getChainOfLoggers(); loggerChain.logMessage(AbstractLogger.INFO, "This is an information."); loggerChain.logMessage(AbstractLogger.DEBUG, "This is an debug level information."); loggerChain.logMessage(AbstractLogger.ERROR, "This is an error information."); } }
Step 4
Verify the output.
Standard Console::Logger: This is an information. File::Logger: This is an debug level information. Standard Console::Logger: This is an debug level information. Error Console::Logger: This is an error information. File::Logger: This is an error information. Standard Console::Logger: This is an error information.Advertisements