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

设计模式-责任链-笔记

动机(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.lryc.cn/news/237257.html

相关文章:

  • SpringMvc请求原理流程
  • 【开源】基于Vue.js的音乐偏好度推荐系统的设计和实现
  • 采集1688整店商品(店铺所有商品、店铺列表api)
  • IObit Unlocker丨解除占用程序软件
  • 开发一款小程序游戏需要多少钱?
  • 基于Vue+SpringBoot的校园电商物流云平台开源项目
  • 庖丁解牛:NIO核心概念与机制详解 03 _ 缓冲区分配、包装和分片
  • 002 OpenCV dft 傅里叶变换
  • 力扣:171. Excel 表列序号(Python3)
  • C++中结构体的初始化
  • vue3+vite+ts 发布自定义组件到npm
  • mybatis使用xml形式配置
  • 开源简历生成器OpenResume
  • AI变现之Gpts搞流量+赚钱
  • 音视频项目—基于FFmpeg和SDL的音视频播放器解析(十六)
  • Elasticsearch文档操作
  • 聊一聊go的单元测试(goconvey、gomonkey、gomock)
  • Positive Technologies 利用 PT Cloud Application Firewall 保护中小型企业的网络资源
  • 深入解析序列模型:全面阐释 RNN、LSTM 与 Seq2Seq 的秘密
  • vue项目本地开发构建速度优化 hard-source-webpack-plugin
  • 燕之屋通过港交所聆讯:苦战IPO十余年,黄健等人提前精准套现
  • 【51单片机系列】C51基础
  • openssl1.0.2版本Windows安装问题
  • 【Java 进阶篇】Ajax 实现——原生JS方式
  • Spring Cloud Stream实践
  • 高精度算法【Java】(待更新中~)
  • 说一说HTTP1.0、1.1、2.0版本区别和优化
  • 51.Sentinel微服务保护
  • 【Java 进阶篇】Ajax 实现——JQuery 实现方式 `ajax()`
  • I.MX6ULL开发笔记(一)——环境搭建、镜像烧录、网络连接