当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
带手续费买卖股票的最大利益[找DP的状态定义到底缺什么?]
MySQL-索引优化和查询优化
修改apt-get源为国内镜像源
【请教】SQL语句按列1去重来计算列2之和
实验7 MPLS实验
跨阻放大器
LeetCode Algorithm 1374. 生成每种字符都是奇数个的字符串
主流定时任务解决方案全横评
SQL server 2014 怎么一次性导出多个查询结果?
埋点开发流程
OC-范畴
新产品立大功 伟世通第二季度营收双增
条件构造器~wapper
获取间隔的日期列表工具类
Mysql报错2003 解决办法 Can‘t connect to MySQL server on ‘localhost‘ (10061)
自然语言处理 文本预处理(上)(分词、词性标注、命名实体识别等)
聊天机器人如何提升独立站的营销水平?
雷达人体存在感应器方案,智能物联网感知技术,实时感应人体存在
Splunk Filed extraction field interception
Swagger的简单介绍,集成,以及如何在生产环境中关闭swagger,在测试和开发环境中自动打开