Chain Of Responsibility Pattern

Chain Of Responsibility Pattern is a behavioral pattern that creates a chain of receiver objects for a request. This pattern decouples sender and receiver of a request based on type of request. 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.

In example below an abstract class AbstractLogger is created with levels of logging defined as a constants. Also, there are 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 passes the message to its next logger.

Chain of Responsibility Pattern UML Diagram

Step 1 : Create an abstract logger class

public abstract class AbstractLogger {
public static int INFO = 1;
public static int DEBUG = 2;
public static int ERROR = 3;

protected int level;

protected AbstractLogger nextLogger;

public void setNextLogger(AbstractLogger nextLogger) {
this.nextLogger = nextLogger;
}

public void logMessage(int level, String message) {
if (this.level <= level) {
write(message);
}
if (nextLogger != null) {
nextLogger.logMessage(level, message);
}
}

protected abstract void write(String message);
}

Step 2 : Create concrete classes extending the logger

public class ConsoleLogger extends AbstractLogger {
public ConsoleLogger(int level) {
this.level = level;
}

@Override
protected void write(String message) {
System.out.println("Standard Console::Logger: " + message);
}
}

public class FileLogger extends AbstractLogger {
public FileLogger(int level) {
this.level = level;
}

@Override
protected void write(String message) {
System.out.println("File::Logger: " + message);
}
}

public class ErrorLogger extends AbstractLogger {
public ErrorLogger(int level) {
this.level = level;
}

@Override
protected void write(String message) {
System.out.println("Error Console::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

public 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;
}

public static void main(String[] args) {
AbstractLogger loggerChain = getChainOfLoggers();

loggerChain.logMessage(AbstractLogger.INFO,
"This is an information");

loggerChain.logMessage(AbstractLogger.DEBUG,
"This is a debug level information");

loggerChain.logMessage(AbstractLogger.ERROR,
"This is an error information");
}
}

The output will be :

Standard Console::Logger: This is an information
File::Logger: This is a debug level information
Standard Console::Logger: This is a 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

Leave a Reply

Your email address will not be published. Required fields are marked *