设计模式-责任链-笔记

动机(Motivation)

在软件构建过程中,一个请求可能被多个对象处理,但是每个请求在运行时只能有个接受者,如果显示指定,将必不可少地带来请求者与接受者的紧耦合。

如何使请求的发送者不需要指定具体的接受者?让请求的接受者自己在运行是决定来处理请求,从而两者解耦。

模式定义:

使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递请求,直到有一个请求处理他为止。

cpp 复制代码
#include <iostream>
#include <string>

using namespace std;

enum class RequestType {
    REQ_HANDLER1,
    REQ_HANDLER2,
    REQ_HANDLER3,
};

class Request {
    string description;
    RequestType reqType;
public:
    Request(const string& desc, RequestType type) 
        : description(desc), reqType(type) {}
    RequestType getReqType() const { return reqType; }
    const string& getDescription() const { return description; }
};

class ChainHandler {
    ChainHandler* nextChain;
    void sendRequestToNextHandler(const Request& req) {
        if (nullptr != nextChain) {
            nextChain->handle(req);
        }
    }
protected:
    virtual bool canHandleRequest(const Request& req) = 0;
    virtual void processRequest(const Request& req) = 0;
public:
    ChainHandler() { nextChain = nullptr; }
    void setNextChain(ChainHandler* next) { nextChain = next; }

    void handle(const Request& req) {
        if (canHandleRequest(req)) {
            processRequest(req);
        }
        else{
            sendRequestToNextHandler(req);
        }
    }
};

class Handler1 : public ChainHandler {
protected:
    virtual bool canHandleRequest(const Request& req) override {
       return req.getReqType() == RequestType::REQ_HANDLER1;
    }
    virtual void processRequest(const Request& req) override {
        cout << "Handler1 is handle request: " << req.getDescription() << endl;
    }
};

class Handler2 : public ChainHandler {
protected:
    virtual bool canHandleRequest(const Request& req) override {
        return req.getReqType() == RequestType::REQ_HANDLER2;
    }
    virtual void processRequest(const Request& req) override {
        cout << "Handler2 is handle request: " << req.getDescription() << endl;
    }
};

class Handler3 : public ChainHandler {
protected:
    virtual bool canHandleRequest(const Request& req) override {
        return req.getReqType() == RequestType::REQ_HANDLER3;
    }
    virtual void processRequest(const Request& req) override {
        cout << "Handler3 is handle request: " << req.getDescription() << endl;
    }
};

int main() {
    Handler1 h1;
    Handler1 h2;
    Handler1 h3;
    h1.setNextChain(&h2);
    h2.setNextChain(&h3);

    Request req("process task ...", RequestType::REQ_HANDLER3);
    h1.handle(req);
    return 0;
}

要点总结:

Chain of Responsibility模式的应用场合在于"一个请求可能有多个接受者,但是最后真正的接受者只有一个",这时候请求发送者与接受者的耦合有可能出现"变化脆弱"的症状,指责链的目的就是将两者解耦,从而更好地应对变化。

应用了Chain of Responsibility模式后,对象的职责分派将更具灵活性。我们可以在运行时动态增加/修改请求的处理职责。

如果请求传递到职责链的末尾乃得不到处理,应该有一个合理的缺省机制。这也使每一个接受对象的职责,而不是发出请求的对象的职责。

相关推荐
_李小白37 分钟前
【OPENGL ES 3.0 学习笔记】延伸阅读:VAO与VBO
笔记·学习·elasticsearch
摇滚侠2 小时前
Spring Boot3零基础教程,Spring Boot 应用打包成 exe 可执行文件,笔记91 笔记92 笔记93
linux·spring boot·笔记
开开心心就好2 小时前
电子报纸离线保存:一键下载多报PDF工具
网络·笔记·macos·pdf·word·音视频·phpstorm
火车叨位去19492 小时前
软件设计模式(tyutJAVA 状态模式实验)
设计模式·状态模式
朝新_2 小时前
【SpringMVC】详解用户登录前后端交互流程:AJAX 异步通信与 Session 机制实战
前端·笔记·spring·ajax·交互·javaee
是Yu欸4 小时前
【博资考5】网安2025
网络·人工智能·经验分享·笔记·网络安全·ai·博资考
报错小能手4 小时前
C++笔记(面向对象)RTTI操作符
开发语言·c++·笔记
CodeLongBear4 小时前
计算机网络学习笔记 | 传输层核心知识点总结(DAY03,匠心制作)
笔记·学习·计算机网络
wdfk_prog5 小时前
[Linux]学习笔记系列 -- [kernel][time]tick
linux·笔记·学习
峰顶听歌的鲸鱼6 小时前
9.OpenStack管理(三)
运维·笔记·分布式·openstack·学习方法