• 设计模式--职责链模式(Chain of Responsibility Pattern)


    职责链模式(Chain of Responsibility Pattern)是一种行为设计模式,它为请求创建了一个接收者对象的链。
    这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。

    在职责链模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象无法处理请求,那么它会把相同的请求传给下一个接收者,依此类推。

    职责链模式主要包含以下角色:

    1. 抽象处理者(Handler):定义了一个处理请求的接口,通常包含一个指向下一个处理者的引用。
    2. 具体处理者(Concrete Handler):具体处理者接收到请求后,可以选择将请求处理掉,或者将请求传给下一个处理者。
    3. 客户类(Client):客户类负责将请求发送到链上,开始请求的处理。

    职责链模式主要解决的问题是,请求的发送者不知道接收者是谁,也不知道请求的处理过程。请求在所有的处理者对象中传递,直到被某个处理者处理。这样实现了请求发送者和接收者之间的解耦。

    以下是一个简单的职责链模式的 C++ 实现:

    #include 
    
    // 抽象处理者
    class Handler {
    public:
        virtual ~Handler() {}
        virtual void setNext(Handler* handler) = 0;
        virtual void handleRequest(int request) = 0;
    };
    
    // 具体处理者
    class ConcreteHandler1 : public Handler {
    public:
        ~ConcreteHandler1() {
            delete next;
        }
        void setNext(Handler* handler) override {
            next = handler;
        }
        void handleRequest(int request) override {
            if (request == 1) {
                std::cout << "ConcreteHandler1 handled the request." << std::endl;
            } else if (next) {
                next->handleRequest(request);
            }
        }
    private:
        Handler* next = nullptr;
    };
    
    class ConcreteHandler2 : public Handler {
    public:
        ~ConcreteHandler2() {
            delete next;
        }
        void setNext(Handler* handler) override {
            next = handler;
        }
        void handleRequest(int request) override {
            if (request == 2) {
                std::cout << "ConcreteHandler2 handled the request." << std::endl;
            } else if (next) {
                next->handleRequest(request);
            }
        }
    private:
        Handler* next = nullptr;
    };
    
    // 客户端代码
    int main() {
        Handler* handler1 = new ConcreteHandler1;
        Handler* handler2 = new ConcreteHandler2;
        handler1->setNext(handler2);
        handler1->handleRequest(1);
        handler1->handleRequest(2);
        delete handler1;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    在这个例子中,ConcreteHandler1 和 ConcreteHandler2 是具体的处理者,它们都继承自抽象处理者 Handler。当接收到请求时,如果 ConcreteHandler1 能处理请求,就处理请求;否则,就将请求传递给下一个处理者 ConcreteHandler2。ConcreteHandler2 的处理逻辑与此类似。

  • 相关阅读:
    SparkSQL项目实战
    TypeScript 泛型
    均匀B样条曲线的表达式
    Win10找不到便签怎么办 Win10找不到便签解决方法
    【Redis底层解析】链表类型
    C++之std::string
    RunwayGen2上线全新控制功能「运动笔刷」
    Java面对对象的特征之二:继承性 :why?
    灌装机的灌装结构设计及仿真(lunwen+任务书+开题+文综+翻译及原文+答辩PPT+cad图纸+UG模型及运动仿真)
    【Django】执行查询——查询JSONField
  • 原文地址:https://blog.csdn.net/lanyang123456/article/details/136129789