当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
获取间隔的日期列表工具类
队列题目:无法吃午餐的学生数量
【ROS基础】map、odom、base_link、laser 的理解 及其 tf 树的理解
jvm 二之 栈帧内部结构
CollectionUtil:一个函数式风格的集合工具
查看僵尸进程
SQL server 2014 怎么一次性导出多个查询结果?
【图像去噪】基于matlab双立方插值和稀疏表示图像去噪【含Matlab源码 2009期】
View port number occupancy
Gradle系列——Gradle插件(基于Gradle文档7.5)day3-2
“蔚来杯“2022牛客暑期多校训练营5,签到题KBGHFCD
交换部分 VLAN
MySQL批量更新
LeetCode 2360. 图中的最长环
结构体大小计算--结构体内存对齐
反射课后习题及做题记录
在VMware上安装Metasploitable2
21 days learning challenge 】 【 sequential search
OC-NSArray
【论文精读】Geometric Structure Preserving Warp for Natural Image Stitching









