当前位置:网站首页>Chain Of Responsibility
Chain Of Responsibility
2022-08-02 07:50:00 【baboon_chen】
Reference:
Design Pattern-Chain of Responsibility Pattern
design-patterns-cpp/Proxy.cpp at master · JakubVojvoda/design-patterns-cpp · GitHub
I. What is the Chain of Responsibility Model?
Definition: The user's request, through disassembly, is passed to the multi-processor for processing in order.They have the same input, and each handler contains the next handler object (the last handler can have no successor), which is ordered like a linked list.In this way, the processing flow of the request can be changed by adjusting the structure of the linked list.
For example, employee reimbursement needs to be signed by the team leader, manager, minister, and accounting bank. If you want to simplify the reimbursement process during special periods, you can remove the approval links such as the team leader and manager;No follow-up process is required.You can even add other approval links, but require all approvers to have the same input, which is the approval form.
Second, Realization
The Chain Of Responsibility model includes the following main roles:
- Handler: A common interface that declares all specific handlers.This interface usually contains only a single method for request handling, but sometimes it also contains a method that sets the next handler on the chain.
- Base Handler: An optional class where you can place sample code common to all handlers.
Typically, this class defines a member variable that holds a reference to the next handler.A client can create a chain by passing a handler to the previous handler's constructor or setter method.This class can also implement the default handling behavior: make sure the next handler exists before passing the request to it.- Concrete Handlers: Contains the actual code that handles the request.After each handler receives a request, it must decide whether to process it, and whether to pass the request down the chain.
The handler is usually independent and immutable, and needs to get all the necessary data at once through the constructor.
/** C++ Design Patterns: Chain of Responsibility* Author: Jakub Vojvoda [github.com/JakubVojvoda]* 2016** Source code is licensed under MIT License* (for more details see LICENSE)**/#include /** Handler: A common interface that declares all concrete handlers.* This interface usually contains only a single method for request processing, but sometimes it also contains a method to set the next handler on the chain*/class Handler{public:virtual ~Handler() {}virtual void setHandler( Handler *s ){successor = s;}virtual void handleRequest(){if (successor != 0){successor->handleRequest();}}// ...private:Handler *successor;};/** Concrete Handlers: Contains the actual code that handles the request.* After each handler receives a request, it must decide whether to process it, and whether to pass the request along the chain.*/class ConcreteHandler1 : public Handler{public:~ConcreteHandler1() {}bool canHandle(){// ...return false;}virtual void handleRequest(){if ( canHandle() ){std::cout << "Handled by Concrete Handler 1" << std::endl;}else{std::cout << "Cannot be handled by Handler 1" << std::endl;Handler::handleRequest();}// ...}// ...};class ConcreteHandler2 : public Handler{public:~ConcreteHandler2() {}bool canHandle(){// ...return true;}virtual void handleRequest(){if ( canHandle() ){std::cout << "Handled by Handler 2" << std::endl;}else{std::cout << "Cannot be handled by Handler 2" << std::endl;Handler::handleRequest();}// ...}// ...};int main(){ConcreteHandler1 handler1;ConcreteHandler2 handler2;handler1.setHandler( &handler2 );handler1.handleRequest();return 0;}
Three, advantages and disadvantages
Benefits
- Similar to the linked list, it is convenient to add, delete, and adjust the processing order.
- Each handler can choose whether to pass the request on.
Disadvantages
- All handlers need to implement the same processing interface, which means, processing the same input.
边栏推荐
猜你喜欢
海缆探测仪TSS350(二)
PWA 踩坑 - 第一次加载页面后无法获取CacheStorage某些资源
主流定时任务解决方案全横评
【故障诊断分析】基于matlab FFT轴承故障诊断(包络谱)【含Matlab源码 2002期】
(2022牛客多校五)D-Birds in the tree(树形DP)
初探形式化方法基本原理
“蔚来杯“2022牛客暑期多校训练营4,签到题NDKHL
正则表达式
System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。不可 访问的日志: Security
(Part of it is not understood, and the notes are not completed) [Graph Theory] Difference Constraints
随机推荐
Mysql报错2003 解决办法 Can‘t connect to MySQL server on ‘localhost‘ (10061)
Link with Game Glitch(spfa判负环)
查看端口号占用
图腾柱和推挽电路介绍
【机器学习】实验1布置:基于决策树的英雄联盟游戏胜负预测
21 days learning challenge 】 【 sequential search
LeetCode Algorithm 1374. 生成每种字符都是奇数个的字符串
论文《Deep Multifaceted Transformers for Multi-objective Ranking in Large-Scale E-commerce Recommender》
【CV】OpenVINO安装教程
2022年数据泄露平均成本高达435万美元,创历史新高!
使用hutool做本地缓存的工具类
实例026:递归求阶乘
正则表达式
堡垒机、堡垒机的原理
MQ带来的一些问题、及解决方案
【ROS基础】rosbag 的使用方法
【云原生】如何快速部署Kubernetes
OC-NSArray
反射课后习题及做题记录
LeetCode SQL 197. 上升的温度