Category : Design Patterns | Sub Category : Behavioral Design Patterns | By Prasad Bonam Last updated: 2023-07-09 09:12:46 Viewed : 600
The Chain of Responsibility pattern is a behavioral design pattern that allows an object to pass a request along a chain of potential handlers until the request is handled or reaches the end of the chain. It decouples the sender of the request from its receivers and provides a way to handle requests dynamically without hardcoding the handler relationships. Here is an example of implementing the Chain of Responsibility pattern in Java:
java// Handler interface
interface Handler {
void setNext(Handler next);
void handleRequest(Request request);
}
// Concrete handler
class ConcreteHandler1 implements Handler {
private Handler next;
@Override
public void setNext(Handler next) {
this.next = next;
}
@Override
public void handleRequest(Request request) {
if (request.getType().equals("Type1")) {
System.out.println("Request handled by ConcreteHandler1");
} else if (next != null) {
next.handleRequest(request);
} else {
System.out.println("Request cannot be handled");
}
}
}
// Concrete handler
class ConcreteHandler2 implements Handler {
private Handler next;
@Override
public void setNext(Handler next) {
this.next = next;
}
@Override
public void handleRequest(Request request) {
if (request.getType().equals("Type2")) {
System.out.println("Request handled by ConcreteHandler2");
} else if (next != null) {
next.handleRequest(request);
} else {
System.out.println("Request cannot be handled");
}
}
}
// Request class
class Request {
private String type;
public Request(String type) {
this.type = type;
}
public String getType() {
return type;
}
}
// Client code
public class Client {
public static void main(String[] args) {
Handler handler1 = new ConcreteHandler1();
Handler handler2 = new ConcreteHandler2();
handler1.setNext(handler2);
Request request1 = new Request("Type1");
handler1.handleRequest(request1); // Output: Request handled by ConcreteHandler1
Request request2 = new Request("Type2");
handler1.handleRequest(request2); // Output: Request handled by ConcreteHandler2
Request request3 = new Request("Type3");
handler1.handleRequest(request3); // Output: Request cannot be handled
}
}
In this example:
Handler
interface declares the common methods that concrete handlers should implement. It includes the methods for setting the next handler in the chain and handling a request.ConcreteHandler1
and ConcreteHandler2
classes are concrete handlers that implement the Handler
interface. Each handler checks if it can handle the request based on its type. If it can handle the request, it performs the necessary operations. If not, it passes the request to the next handler in the chain, if available.Request
class represents the request object that is passed along the chain. It encapsulates the type of the request.Client
class demonstrates the usage of the chain of responsibility. It creates instances of the concrete handlers (ConcreteHandler1
and ConcreteHandler2
) and sets the next handler in the chain. It then creates requests of different types and passes them to the first handler in the chain using the handleRequest()
method.By using the Chain of Responsibility pattern, you can achieve loose coupling between the sender of a request and its receivers. Each receiver has the opportunity to handle the request or pass it to the next receiver in the chain. This pattern promotes flexibility, extensibility, and the ability to dynamically change the handling order or add new handlers without modifying the client code.