Chain Responsibility Pattern

Chain Responsibility Pattern

1. Allows a request to be passed along the chain of handlers until it is processed.

2. used in Qt

2.1 Qt Event loop

2.2 Qt Event Loop Call Stack, using a mouse click as an example.

复制代码
QWidgetWindow::handleMouseEvent
 
QWidget * QApplicationPrivate::pickMouseReceiver
 
QApplication::notify(QObject *receiver, QEvent *e)
 
while (w) {
 
 res = d->notify_helper(...);
 
 w = w->parentWidget();
 
}
 
QApplicationPrivate::notify_helper(QObject *receiver, QEvent * e)
 
QWidget::event(event);

3. example

3.1 Handler
复制代码
#include <iostream>
#include <memory>
#include <string>

// 抽象处理者类
class Handler {
public:
    virtual ~Handler() = default;
    
    // 设置下一个处理者
    void setNext(std::shared_ptr<Handler> next) {
        nextHandler = next;
    }

    // 处理请求
    virtual void handleRequest(const std::string& request) = 0;

protected:
    std::shared_ptr<Handler> nextHandler;
};

3.2 ConcreteHandler

复制代码
// 具体处理者1
class ConcreteHandlerA : public Handler {
public:
    void handleRequest(const std::string& request) override {
        if (request == "A") {
            std::cout << "Handler A is handling the request." << std::endl;
        } else if (nextHandler) {
            nextHandler->handleRequest(request);
        } else {
            std::cout << "Request not handled." << std::endl;
        }
    }
};

// 具体处理者2
class ConcreteHandlerB : public Handler {
public:
    void handleRequest(const std::string& request) override {
        if (request == "B") {
            std::cout << "Handler B is handling the request." << std::endl;
        } else if (nextHandler) {
            nextHandler->handleRequest(request);
        } else {
            std::cout << "Request not handled." << std::endl;
        }
    }
};

// 具体处理者3
class ConcreteHandlerC : public Handler {
public:
    void handleRequest(const std::string& request) override {
        if (request == "C") {
            std::cout << "Handler C is handling the request." << std::endl;
        } else if (nextHandler) {
            nextHandler->handleRequest(request);
        } else {
            std::cout << "Request not handled." << std::endl;
        }
    }
};

3.3 main

复制代码
int main() {
    // 创建具体的处理者
    std::shared_ptr<Handler> handlerA = std::make_shared<ConcreteHandlerA>();
    std::shared_ptr<Handler> handlerB = std::make_shared<ConcreteHandlerB>();
    std::shared_ptr<Handler> handlerC = std::make_shared<ConcreteHandlerC>();

    // 设置责任链
    handlerA->setNext(handlerB);
    handlerB->setNext(handlerC);

    // 发起请求
    std::cout << "Sending request A:" << std::endl;
    handlerA->handleRequest("A");

    std::cout << "\nSending request B:" << std::endl;
    handlerA->handleRequest("B");

    std::cout << "\nSending request C:" << std::endl;
    handlerA->handleRequest("C");

    std::cout << "\nSending request D:" << std::endl;
    handlerA->handleRequest("D");

    return 0;
}

3.4 result

复制代码
Sending request A:
Handler A is handling the request.

Sending request B:
Handler B is handling the request.

Sending request C:
Handler C is handling the request.

Sending request D:
Request not handled.
相关推荐
Kevinyu_2 天前
责任链模式
java·hadoop·责任链模式
明洞日记2 天前
【设计模式手册012】责任链模式 - 请求处理的流水线艺术
java·设计模式·责任链模式
WKP941811 天前
过滤器模式、责任链模式
责任链模式
ZHE|张恒12 天前
设计模式实战篇(五):责任链模式 — 把复杂审批/过滤流程变成可组合的“传递链”
设计模式·责任链模式
CodeAmaz12 天前
使用责任链模式设计电商下单流程(Java 实战)
java·后端·设计模式·责任链模式·下单
海棠Flower未眠24 天前
SpringBoot 项目基于责任链模式实现复杂接口的解耦和动态编排
责任链模式
Query*1 个月前
Java 设计模式—— 责任链模式:从原理到 SpringBoot 最优实现
java·spring boot·责任链模式
凸头1 个月前
责任链模式
java·开发语言·责任链模式
Mr_WangAndy1 个月前
C++设计模式_行为型模式_责任链模式Chain of Responsibility
c++·设计模式·责任链模式·行为型模式
星空寻流年1 个月前
设计模式第七章(责任链模式)
设计模式·责任链模式