Mastering Request Routing with the Chain of Responsibility Pattern
In this blog I am going to explain chain of responsibility design pattern . This design Pattern comes under behavioural design pattern. I have tried to keep it simple.
Chain of Responsibility Design Pattern
When?
How?
What?
Chain of responsibility is a behavioural design pattern used when a request can be fulfilled by one or more entity(handler). Who will handle the request this question is unknown in advance.
When?Use the design pattern when itβs essential to execute several handlers in a particular order. i.e in a logger system let's assume we want to log in order Info -> Debug -> Error . If a log mentioned of level error it should be handled by info and debug and error but if the level is debug it should be handled by info and debug only.
How ?
To understand the chain of responsibility design pattern let's understand by designing a logging framework .
Problem Statement: Design a logging framework.
Requirement:
- It should support 3 log levels info, debug, error.
- If log level is error it should get printed on all three levels.
- If log level is debug it should get printed on info and debug level.
- If log level is info it should get printed for info level only.
Class Diagram
Let's code it
Interface
public interface Logger {
void log(String logLevel, String str); void pass(String logLevel, String str);}Abstract Class
abstract class BaseLogger implements Logger{
Logger nextLogger; public BaseLogger(Logger nextLogger){ this.nextLogger = nextLogger; } public void log(String logLevel, String str) { System.out.println("Logged in" +logLevel+ "level"); } public void pass(String logLevel, String str){ this.nextLogger.log(logLevel, str); }}InfoLogger Class
public class InfoLogger extends BaseLogger { public InfoLogger(Logger nextLogger) { super(nextLogger); } public void log(String level, String str){ System.out.println("Logged in Info level"); if(level != "info")pass(level, str); }}DebugLogger Class
public class DebugLogger extends BaseLogger{ public DebugLogger(Logger nextLogger) { super(nextLogger); } public void log(String level, String str){ System.out.println("Logged in debug"); if(level != "debug")pass(level, str); }}ErrorLogger Class
public class ErrorLogger extends BaseLogger{ public ErrorLogger(Logger nextLogger) { super(nextLogger); } public void log(String level, String str){ System.out.println("Logged in Error level"); if(level != "error")pass(level, str); }}Client
public class Demo { public static void main(String [] args){ Logger loggert = new InfoLogger(new DebugLogger(new ErrorLogger(null))); loggert.log("error", "Logging"); }}Conclusion
In conclusion, the Chain of Responsibility pattern offers several advantages:
- Flexibility in Handling Requests
- Enhanced Maintainability
- Reduced Coupling
- Improved Readability
However, there are also potential drawbacks to consider:
- Performance Overhead
- Complex Debugging
Frequently Asked Questions
Explore our complete guide
Software Engineering Career RoadmapFrom junior developer to engineering manager β guides, experiences, and advice from senior engineers on building a long-term tech career.