当前位置:网站首页>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.
边栏推荐
猜你喜欢

【故障诊断分析】基于matlab FFT轴承故障诊断(包络谱)【含Matlab源码 2002期】

论文阅读 (64):Weakly-supervised Video Anomaly Detection with Robust Temporal Feature Magnitude Learning

Facebook社媒营销的5大技巧,迅速提高独立站转化率!

Enterprise training and reproduction guidebook - training and reasoning of the OpenPose model based on Huawei ModelArts platform, realizing the recognition of two behaviors of climbing and climbing ov

【CNN回归预测】基于matlab卷积神经网络CNN数据回归预测【含Matlab源码 2003期】

张驰咨询:企业实施精益管理的最大障碍,只把精益作为一种工具和方法

FormData上传二进制文件、对象、对象数组

【图像去噪】基于matlab双立方插值和稀疏表示图像去噪【含Matlab源码 2009期】

实例026:递归求阶乘

2022.07.31(LC_6132_使数组中所有元素都等于零)
随机推荐
hdu1752 copy
敏捷、DevOps和嵌入式系统测试
optional
【ROS基础】rosbag 的使用方法
请教一下,Flink SQL ,JDBC sink 入 mysql 库,想要搞一个自增主键,要怎么写
【网络】IP、子网掩码
交换部分 VLAN
实例027:递归输出
OC-Category
apt & apt-get命令
OC-NSString
【图像隐藏】基于matlab混合DWT-HD-SVD数字图像水印方法技术【含Matlab源码 2007期】
线程的创建方式
SimpleChannelInboundHandler使用总结
Ask a question, my Flinkcdc has run through, I can monitor the binlog of msql, and I can also send kafk
(2022牛客多校五)B-Watches(二分)
埋点开发流程
View port number occupancy
View zombie processes
MySQL-索引优化和查询优化