当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
(2022牛客多校五)B-Watches(二分)
MySQL批量更新
如何设计静态资源缓存方案
图腾柱和推挽电路介绍
sql 远程访问链接服务器
自然语言处理 文本预处理(下)(张量表示、文本数据分析、文本特征处理等)
第06章 索引的数据结构【2.索引及调优篇】【MySQL高级】
张驰课堂:六西格玛测量系统的误差分析与判定
MySQL-索引优化和查询优化
逆变器绝缘检测检测功能及软件实现
查找最大的n个文件
海缆探测仪TSS350(二)
LeetCode 2360. 图中的最长环
Splunk Filed extraction field interception
OC-NSArray
概率论与数理统计
_2_顺序表
OC-NSDictionary
正则表达式的理解学习
Splunk Filed extraction 字段截取