当前位置: 首页 > article >正文

设计模式-责任链-笔记

动机(Motivation)

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

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

模式定义:

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

#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模式后,对象的职责分派将更具灵活性。我们可以在运行时动态增加/修改请求的处理职责。

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


http://www.kler.cn/news/135573.html

相关文章:

  • 【Web】Ctfshow SSRF刷题记录1
  • 程序员开发者神器:10个.Net开源项目
  • Leetcode—206.反转链表【简单】
  • java基于RestTemplate的微服务发起http请求
  • k8s运维管理
  • Flutter笔记:桌面应用 窗口定制库 bitsdojo_window
  • WIFI版本云音响设置教程腾讯云平台版本
  • 基于SSM的供电公司安全生产考试系统设计与实现
  • MATLAB 嵌套switch语句||MATLAB while循环
  • C++中只能有一个实例的单例类
  • LeetCode Hot100之十:239.滑动窗口最大值
  • 网络运维与网络安全 学习笔记2023.11.19
  • 【Go学习之 go mod】gomod小白入门,在github上发布自己的项目(项目初始化、项目发布、项目版本升级等)
  • 世界坐标系,相机坐标系,像素坐标系转换 详细说明(附代码)
  • PCIe协议加持,SD卡9.1规范达到媲美SSD的速度4GB/s
  • 【设计模式】聊聊模板模式
  • 解析Spring Boot中的CommandLineRunner和ApplicationRunner:用法、区别和适用场景详解
  • CISP全真模式测试题(二)
  • VSCode使用
  • 基于Vue+SpringBoot的超市账单管理系统 开源项目
  • CentOS 7.9 安装 epel-release
  • cobol基本语法
  • k8s-部署Redis-cluster(TLS)
  • 【React】React 基础
  • 基础算法:高精度加法
  • C语言之深入指针及qsort函数(五)(详解介绍)
  • 微信小程序内嵌h5页面,实现动态设置顶部标题的功能
  • ArkTS - HarmonyOS服务卡片(创建)
  • CISP模拟试题(一)
  • uniapp+vue+Springboot 公司网站0~1搭建 前端前期设计篇