责任链模式(Chain of Responsibility Pattern)

title

责任链模式(Chain of Responsibility Pattern)是一种行为型设计模式,它允许将请求沿着处理者链传递,直到有一个处理者能够处理该请求为止。

在责任链模式中,通常由多个处理对象组成一个处理链。当一个请求被发起时,它会沿着这个处理链依次经过每个处理对象,直到有一个处理对象能够处理该请求为止。如果整个处理链上的所有处理对象都无法处理该请求,那么该请求将被拒绝。

责任链模式可以用来解耦请求的发送者和接收者之间的关系。它还可以避免请求的发送者和接收者之间的耦合,因为请求发送者不需要知道处理请求的对象是谁,而处理者也不需要知道请求的发送者是谁。

在实现责任链模式时,通常需要定义一个抽象处理者(Handler)类,所有具体处理者都继承自该类。抽象处理者类中通常定义了一个指向下一个处理者的引用(即链中的下一个处理对象),并且定义了一个处理请求的抽象方法。具体处理者在实现该抽象方法时,可以决定是否自己处理该请求,或者将请求转发给下一个处理者。同时,还需要在客户端代码中创建处理对象,并将它们组合成一条处理链。

责任链模式的优点是可以动态地添加或删除处理对象,而且可以灵活地配置处理链。同时,它也有一些缺点,比如处理请求的速度较慢,因为请求需要依次经过多个处理对象。此外,如果处理链过长或者处理对象过多,可能会导致系统性能下降。

下面是一个简单的 Java 实现责任链模式的示例:


// 定义抽象处理器类
abstract class Handler {
    private Handler nextHandler; // 下一个处理器

    public Handler(Handler nextHandler) {
        this.nextHandler = nextHandler;
    }

    // 处理请求的抽象方法
    public abstract void handleRequest(Request request);

    // 获取下一个处理器
    public Handler getNextHandler() {
        return nextHandler;
    }
}

// 定义具体处理器类
class ConcreteHandlerA extends Handler {
    public ConcreteHandlerA(Handler nextHandler) {
        super(nextHandler);
    }

    @Override
    public void handleRequest(Request request) {
        if (request.getType() == RequestType.TYPEA) {
            System.out.println("ConcreteHandlerA 处理请求:" + request.getContent());
        } else {
            // 转发请求给下一个处理器
            Handler nextHandler = getNextHandler();
            if (nextHandler != null) {
                nextHandler.handleRequest(request);
            }
        }
    }
}

class ConcreteHandlerB extends Handler {
    public ConcreteHandlerB(Handler nextHandler) {
        super(nextHandler);
    }

    @Override
    public void handleRequest(Request request) {
        if (request.getType() == RequestType.TYPEB) {
            System.out.println("ConcreteHandlerB 处理请求:" + request.getContent());
        } else {
            // 转发请求给下一个处理器
            Handler nextHandler = getNextHandler();
            if (nextHandler != null) {
                nextHandler.handleRequest(request);
            }
        }
    }
}

// 定义请求类
class Request {
    private RequestType type;
    private String content;

    public Request(RequestType type, String content) {
        this.type = type;
        this.content = content;
    }

    public RequestType getType() {
        return type;
    }

    public String getContent() {
        return content;
    }
}

// 定义请求类型枚举
enum RequestType {
    TYPEA, TYPEB
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        // 创建处理器链
        Handler handlerA = new ConcreteHandlerA(null);
        Handler handlerB = new ConcreteHandlerB(handlerA);

        // 发送请求
        Request requestA = new Request(RequestType.TYPEA, "请求类型A");
        Request requestB = new Request(RequestType.TYPEB, "请求类型B");
        handlerB.handleRequest(requestA);
        handlerB.handleRequest(requestB);
    }
}

在这个示例中,抽象处理器类 Handler 定义了一个指向下一个处理器的引用,并且定义了一个处理请求的抽象方法 handleRequest。具体处理器类 ConcreteHandlerA 和 ConcreteHandlerB 继承自抽象处理器类,并在实现 handleRequest 方法时,根据请求类型决定是否自己处理该请求,或者将请求转发给下一个处理器。在客户端代码中,我们创建处理器链,然后发送不同类型的请求,并交由处理器链来处理。

责任链模式的UML图如下:

+----------------+            +----------------------+
|    Handler     |<---------->|   ConcreteHandlerA   |
+----------------+            +----------------------+
| - nextHandler: Handler |    | - handleRequest(request: Request): void |
+----------------+            +----------------------+
                               /         \
                              /           \
                             /             \
            +----------------------+          +-----------------------+
            |   ConcreteHandlerB   |<---------|    ConcreteHandlerC   |
            +----------------------+          +-----------------------+
            | - handleRequest(request: Request): void |
            +----------------------+
powered by Gitbook© 2023 编外计划 | 最后修改: 2023-11-24 03:37:01

results matching ""

    No results matching ""