Java chain of responsibility design pattern

Java chain of responsibility design pattern comes under behavioural design patterns. As the name suggest chain of responsibility design pattern a request will be send to the chain of objects. The request will be handled by first object and if it is not able to handle that request, request will be send to next object in the chain and so on.

We can take the example of multiple try catch blocks example. Every catch block will act as an object to handle the request.

Let’s discuss chain of responsibility design pattern with below example. We will implementing logger levels with chain of responsibility design pattern.

Example

Logger.java

package com.w3schools;
 
public abstract class Logger {
    public static int INFO=1;  
    public static int ERROR=2;  
    public static int DEBUG=3;  
    protected int level;  
    protected Logger nextLevelLogger;  
    public void setNextLevelLogger(Logger nextLevelLogger) {  
        this.nextLevelLogger = nextLevelLogger;  
    }  
    public void logMessage(int levels, String message){  
        if(this.level<=level){  
            displayLogInfo(message);  
        }  
        if (nextLevelLogger!=null) {  
            nextLevelLogger.logMessage(levels, message);  
        }  
    }  
    protected abstract void displayLogInfo(String message);  
}

ConsoleLogger.java

package com.w3schools;
 
public class ConsoleLogger extends Logger {
        public ConsoleLogger(int level){
          this.level = level;
        }
 
	@Override
	protected void displayLogInfo(String message) {
		System.out.println("Console logger: " + message);	
	}
 
}

DebugLogger.java

package com.w3schools;
 
public class DebugLogger extends Logger {
        public DebugLogger(int level){
           this.level = level;
        }
 
	@Override
	protected void displayLogInfo(String message) {
		System.out.println("Debug logger: " + message);	
	}
 
}

ErrorLogger.java

package com.w3schools;
 
public class ErrorLogger extends Logger {
        public ErrorLogger(int level){
            this.level = level;
        }
 
	@Override
	protected void displayLogInfo(String message) {
		System.out.println("Error logger: " + message);	
	}
 
}

ChainOfResponsibilityPatternTest.java

package com.w3schools;
 
public class ChainOfResponsibilityPatternTest {
	private static Logger getChainOfLoggers(){
	    Logger errorLogger = new ErrorLogger(Logger.ERROR);
	    Logger debugLogger = new DebugLogger(Logger.DEBUG);
	    Logger consoleLogger = new ConsoleLogger(Logger.INFO);
 
	    errorLogger.setNextLevelLogger(debugLogger);
	    debugLogger.setNextLevelLogger(consoleLogger);
 
	    return errorLogger;	
	}
	public static void main(String args[]){
		Logger loggerChain = getChainOfLoggers();
		loggerChain.logMessage(Logger.INFO, "Information message.");
		loggerChain.logMessage(Logger.DEBUG, "Debug message.");
		loggerChain.logMessage(Logger.ERROR, "Error message.");
	}
}

Output

Error logger: Information message.
Debug logger: Information message.
Console logger: Information message.
Error logger: Debug message.
Debug logger: Debug message.
Console logger: Debug message.
Error logger: Error message.
Debug logger: Error message.
Console logger: Error message.