CHAIN OF RESPONSIBILITY PATTERN
[Link] Copyright © [Link]
As the name suggests, the chain of responsibility 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.
[Link]
public abstract class AbstractLogger {
public static int INFO = 1;
public static int DEBUG = 2;
public static int ERROR = 3;
protected int level;
//next element in chain or responsibility
protected AbstractLogger nextLogger;
public void setNextLogger(AbstractLogger nextLogger){
[Link] = nextLogger;
}
public void logMessage(int level, String message){
if([Link] <= level){
write(message);
}
if(nextLogger !=null){
[Link](level, message);
}
}
abstract protected void write(String message);
Step 2
Create concrete classes extending the logger.
[Link]
public class ConsoleLogger extends AbstractLogger {
public ConsoleLogger(int level){
[Link] = level;
}
@Override
protected void write(String message) {
[Link]("Standard Console::Logger: " + message);
}
}
[Link]
public class ErrorLogger extends AbstractLogger {
public ErrorLogger(int level){
[Link] = level;
}
@Override
protected void write(String message) {
[Link]("Error Console::Logger: " + message);
}
}
[Link]
public class FileLogger extends AbstractLogger {
public FileLogger(int level){
[Link] = level;
}
@Override
protected void write(String message) {
[Link]("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.
[Link]
public class ChainPatternDemo {
private static AbstractLogger getChainOfLoggers(){
AbstractLogger errorLogger = new ErrorLogger([Link]);
AbstractLogger fileLogger = new FileLogger([Link]);
AbstractLogger consoleLogger = new ConsoleLogger([Link]);
[Link](fileLogger);
[Link](consoleLogger);
return errorLogger;
}
public static void main(String[] args) {
AbstractLogger loggerChain = getChainOfLoggers();
[Link]([Link],
"This is an information.");
[Link]([Link],
"This is an debug level information.");
[Link]([Link],
"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.