责任链模式深入理解及java demo
时间: 2023-10-07 20:06:20 浏览: 103
深入浅出JAVA责任链模式
责任链模式是一种行为型设计模式,它允许你将请求沿着处理者链进行发送,直到其中一个处理者能够处理它为止。每个处理者都有一个对下一个处理者的引用,形成了一个链式结构。
这种模式的优点在于它可以动态地组合处理者链,而不需要修改现有代码。它还允许您以灵活的方式为请求选择处理者。
下面是一个简单的Java示例,演示了如何使用责任链模式。我们将创建一个处理器链,每个处理器都能处理特定类型的请求:
首先,我们需要定义一个请求类,它将包含要处理的数据:
```java
public class Request {
private String requestType;
private String requestData;
public Request(String requestType, String requestData) {
this.requestType = requestType;
this.requestData = requestData;
}
public String getRequestType() {
return requestType;
}
public String getRequestData() {
return requestData;
}
}
```
接下来,我们需要定义一个处理器接口,它将定义处理请求的方法:
```java
public interface RequestHandler {
void handleRequest(Request request);
}
```
然后,我们将实现几个具体处理器类,分别处理不同类型的请求:
```java
public class EmailHandler implements RequestHandler {
private RequestHandler nextHandler;
public void setNextHandler(RequestHandler handler) {
nextHandler = handler;
}
public void handleRequest(Request request) {
if (request.getRequestType().equals("email")) {
System.out.println("EmailHandler handles request: " + request.getRequestData());
} else if (nextHandler != null) {
nextHandler.handleRequest(request);
} else {
System.out.println("No handler found for request: " + request.getRequestType());
}
}
}
public class FaxHandler implements RequestHandler {
private RequestHandler nextHandler;
public void setNextHandler(RequestHandler handler) {
nextHandler = handler;
}
public void handleRequest(Request request) {
if (request.getRequestType().equals("fax")) {
System.out.println("FaxHandler handles request: " + request.getRequestData());
} else if (nextHandler != null) {
nextHandler.handleRequest(request);
} else {
System.out.println("No handler found for request: " + request.getRequestType());
}
}
}
public class LetterHandler implements RequestHandler {
private RequestHandler nextHandler;
public void setNextHandler(RequestHandler handler) {
nextHandler = handler;
}
public void handleRequest(Request request) {
if (request.getRequestType().equals("letter")) {
System.out.println("LetterHandler handles request: " + request.getRequestData());
} else if (nextHandler != null) {
nextHandler.handleRequest(request);
} else {
System.out.println("No handler found for request: " + request.getRequestType());
}
}
}
```
最后,我们将使用这些处理器类创建一个处理器链,并将请求发送到该链:
```java
public class ChainDemo {
private static RequestHandler getChainOfHandlers() {
RequestHandler emailHandler = new EmailHandler();
RequestHandler faxHandler = new FaxHandler();
RequestHandler letterHandler = new LetterHandler();
emailHandler.setNextHandler(faxHandler);
faxHandler.setNextHandler(letterHandler);
return emailHandler;
}
public static void main(String[] args) {
RequestHandler chain = getChainOfHandlers();
chain.handleRequest(new Request("email", "Email request"));
chain.handleRequest(new Request("fax", "Fax request"));
chain.handleRequest(new Request("letter", "Letter request"));
chain.handleRequest(new Request("unknown", "Unknown request"));
}
}
```
当我们运行该程序时,它将输出以下结果:
```
EmailHandler handles request: Email request
FaxHandler handles request: Fax request
LetterHandler handles request: Letter request
No handler found for request: unknown
```
这个例子演示了如何使用责任链模式来处理请求。在这个例子中,我们创建了一个处理器链,每个处理器都可以处理特定类型的请求。当请求到达链的顶部时,它将沿着链向下传递,直到找到可以处理它的处理器为止。如果没有处理器能够处理请求,则会输出一条错误消息。
总之,责任链模式可以让您以灵活和可扩展的方式组织代码,并将请求发送到处理器链中,直到找到可以处理它的处理器为止。这种模式可以用于许多不同的场景,例如请求处理、日志记录和异常处理。
阅读全文