Chain Of Responsibility Pattern
## Chain of Responsibility Pattern
The Chain of Responsibility Pattern creates a chain of receiver objects for a request. This pattern decouples the sender and receiver of a request based on the type of request. This type of design pattern falls under behavioral patterns.
The Chain of Responsibility Pattern connects multiple handlers (processing objects) in a chain structure, allowing a request to pass along this chain until one of the handlers processes it.
The Chain of Responsibility Pattern allows multiple objects the opportunity to handle a request, thereby avoiding the coupling between the sender and the receiver of the request. These objects are connected into a chain, and the request is passed along this chain.
### Intent
Allow a request to be passed along a chain of handlers until it is handled.
### Main Problem Solved
* Decouples the request sender and receiver, allowing multiple objects to potentially receive the request without the sender needing to know which object will handle it.
### Use Cases
* When multiple objects can handle a request, and the specific handler is determined at runtime.
* When you need to submit a request to one of multiple objects without explicitly specifying the receiver.
### Implementation Approach
* **Define the Handler Interface**: All handlers must implement the same interface.
* **Create Concrete Handlers**: Concrete classes that implement the interface, containing the request-handling logic and a reference to the next handler in the chain.
### Key Code
* **Handler Interface**: Defines a method for handling requests.
* **ConcreteHandler Class**: Implements the Handler interface, containing request-handling logic and a reference to the next handler.
### Application Examples
1. **Pass the Parcel**: The passing action in the game continues until the music stops.
2. **Event Bubbling**: In JavaScript, events start from the most specific element and propagate upward level by level.
3. **Web Servers**: Such as Apache Tomcat handling character encoding, Struts2 interceptors, and Servlet Filters.
### Advantages
1. **Reduces Coupling**: Decouples the sender and receiver.
2. **Simplifies Objects**: Objects do not need to know the structure of the chain.
3. **Flexibility**: Responsibilities can be dynamically added or removed by changing the members or order of the chain.
4. **Easy to Extend**: Adding new request-handling classes is convenient.
### Disadvantages
1. **Request May Not Be Handled**: There is no guarantee that a request will be received by a handler in the chain.
2. **Performance Impact**: May affect system performance, and debugging can be difficult, potentially leading to circular calls.
3. **Hard to Observe**: Runtime characteristics are not obvious, which may hinder debugging.
### Usage Suggestions
* When handling requests, if there are multiple potential handlers, consider using the Chain of Responsibility pattern.
* Ensure each handler in the chain clearly knows how to pass the request to the next link in the chain.
### Notes
* In Java Web development, the Chain of Responsibility pattern is widely used, such as in filter chains and interceptors.
### Structure
**The main core roles involved are:**
* **Abstract Handler (Handler):**
* Defines an interface for handling requests, typically including a method for handling requests (e.g., `handleRequest`) and a reference to the next handler (successor).
* **Concrete Handler (ConcreteHandler):**
* Implements the abstract handler interface and is responsible for handling requests. If it can handle the request, it processes it directly; otherwise, it passes the request to the next handler.
* **Client (Client):**
* Creates handler objects and connects them into a chain of responsibility. Typically, the client only needs to send the request to the first handler in the chain without worrying about the specific processing of the request.
We create an abstract class _AbstractLogger_ with detailed logging levels. Then we create three types of loggers, all extending _AbstractLogger_. Each logger checks if the message level belongs to its own level; if so, it prints it accordingly; otherwise, it does not print and passes the message to the next logger.

### Step 1
Create the abstract logger class.
## AbstractLogger.java
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); }}abstract protected void write(String message); }
### Step 2
Create entity classes that extend the logger class.
## ConsoleLogger.java
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); }}
## ErrorLogger.java
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); }}
## FileLogger.java
public class FileLogger extends AbstractLogger{public 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 different error levels and set the next logger in each logger. The next logger in each logger represents part of the chain.
## ChainPatternDemo.java
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."); }}
### Step 4
Execute the program and output the results:
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.
YouTip