Chain of Responsibility
It allow multiple objects to handle a request, where each object in the chain has the option to either process the request or pass it to the next handler in the chain. This pattern helps in decoupling the sender of a request from its receiver, allowing multiple handlers to process the request in a flexible manner.
Structure
- Handler: Defines an interface for handling requests and an optional method to set the next handler in the chain.
- Concrete Handler: Implements the handler interface and processes the request. It can also forward the request to the next handler if it cannot handle it.
- Client: Sends the request to the handler and does not know which object in the chain will handle it.
Example
// Handler interface
interface SupportHandler {
void setNextHandler(SupportHandler nextHandler); // Set the next handler in the chain
void handleRequest(String request); // Handle the request
}
// Concrete handler for low-level support
class LowLevelSupportHandler implements SupportHandler {
private SupportHandler nextHandler;
@Override
public void setNextHandler(SupportHandler nextHandler) {
this.nextHandler = nextHandler;
}
@Override
public void handleRequest(String request) {
if (request.equals("LowLevel")) {
System.out.println("LowLevelSupportHandler: Handling low-level support request.");
} else if (nextHandler != null) {
System.out.println("LowLevelSupportHandler: Passing request to the next handler.");
nextHandler.handleRequest(request);
}
}
}
// Concrete handler for mid-level support
class MidLevelSupportHandler implements SupportHandler {
private SupportHandler nextHandler;
@Override
public void setNextHandler(SupportHandler nextHandler) {
this.nextHandler = nextHandler;
}
@Override
public void handleRequest(String request) {
if (request.equals("MidLevel")) {
System.out.println("MidLevelSupportHandler: Handling mid-level support request.");
} else if (nextHandler != null) {
System.out.println("MidLevelSupportHandler: Passing request to the next handler.");
nextHandler.handleRequest(request);
}
}
}
// Concrete handler for high-level support
class HighLevelSupportHandler implements SupportHandler {
private SupportHandler nextHandler;
@Override
public void setNextHandler(SupportHandler nextHandler) {
this.nextHandler = nextHandler;
}
@Override
public void handleRequest(String request) {
if (request.equals("HighLevel")) {
System.out.println("HighLevelSupportHandler: Handling high-level support request.");
} else {
System.out.println("HighLevelSupportHandler: Cannot handle the request.");
}
}
}
// Client code
public class Main {
public static void main(String[] args) {
// Creating the chain of responsibility
SupportHandler lowLevel = new LowLevelSupportHandler();
SupportHandler midLevel = new MidLevelSupportHandler();
SupportHandler highLevel = new HighLevelSupportHandler();
// Setting the chain
lowLevel.setNextHandler(midLevel);
midLevel.setNextHandler(highLevel);
// Client sends requests
System.out.println("Client sends a LowLevel request:");
lowLevel.handleRequest("LowLevel");
System.out.println("\nClient sends a MidLevel request:");
lowLevel.handleRequest("MidLevel");
System.out.println("\nClient sends a HighLevel request:");
lowLevel.handleRequest("HighLevel");
System.out.println("\nClient sends an unknown request:");
lowLevel.handleRequest("Unknown");
}
}