文章目录
责任链模式
定义
责任链模式(Chain of Responsibility)使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有对象能够处理它。
使用场景
链式流程化处理。
例如请假审批流程。
基本思路
创建请求(request 待处理数据)创建多个处理节点(processor),每个节点指定其下一个处理节点。
在起始节点传入请求,最后会通过处理节点条件 判断会有哪些节点处理。
例如:请假审批,[0,1] 天要有Leader1审批
[1,7] 天要有Leader1,Leader2审批
[7,30] 要有Leader1,Leader2,Leader3审批
比如张三请教5天,正常流程需要Leader1,Leader2
张三请教8天,正常流程需要Leader1,Leader2,Leader3
当然这个处理流程可以根据 handlerRequest 的处理条件 来改变。
实现
#ifndef ABSTRACTPROCESSOR_H #define ABSTRACTPROCESSOR_H #include <LeaveRequest.h> class AbstractProcessor{ public: /**直接主管审批处理的请假天数**/ const static int MIN = 1; /**部门经理处理的请假天数**/ const static int MIDDLE = 7; /**总经理处理的请假天数**/ const static int MAX = 30; /**领导名称*/ std::string handlerName; /**下一个处理节点(即更高级别的领导)**/ AbstractProcessor* nextHandler = nullptr; /**设置下一节点**/ void setNextHandler(AbstractProcessor *handler){ this->nextHandler = handler; } /**处理请假的请求,子类实现**/ virtual void handlerRequest(LeaveRequest* request) = 0; }; #endif // ABSTRACTPROCESSOR_H
#ifndef LEADER1PROCESSOR_H #define LEADER1PROCESSOR_H #include <AbstractProcessor.h> #include <stdio.h> class Leader1Processor:public AbstractProcessor{ public: void handlerRequest(LeaveRequest* request) override{ if(request->m_iLeaveDays >= AbstractProcessor::MIN){ printf("Leader1:%s,proceess,finished!\n",handlerName.c_str()); if(nullptr != this->nextHandler){ printf("Leader1 ->next \n"); this->nextHandler->handlerRequest(request); }else{ printf("process reject\n"); } }else{ printf("not %s duty\n",handlerName.c_str()); return; } } }; #endif // LEADER1PROCESSOR_H
#ifndef LEADER2PROCESSOR_H #define LEADER2PROCESSOR_H #include <AbstractProcessor.h> #include <stdio.h> class Leader2Processor:public AbstractProcessor{ public: void handlerRequest(LeaveRequest* request) override{ if(request->m_iLeaveDays >=AbstractProcessor::MIDDLE){ printf("Leader2:%s,proceess,finished!\n",handlerName.c_str()); if(nullptr != this->nextHandler){ printf("Leader2 ->next \n"); this->nextHandler->handlerRequest(request); }else{ printf("process reject;\n"); } }else{ printf("not %s duty\n",handlerName.c_str()); return; } } }; #endif // LEADER1PROCESSOR_H
#ifndef LEADER3PROCESSOR_H #define LEADER3PROCESSOR_H #include <AbstractProcessor.h> #include <stdio.h> class Leader3Processor:public AbstractProcessor{ public: void handlerRequest(LeaveRequest* request) override{ if(request->m_iLeaveDays<=AbstractProcessor::MAX){ printf("Leader3:%s,proceess,finished!\n",handlerName.c_str()); if(nullptr != this->nextHandler){ printf("Leader3 ->next \n"); this->nextHandler->handlerRequest(request); }else{ printf("process end;\n"); } }else{ printf("not %s duty\n",handlerName.c_str()); return; } } }; #endif // LEADER1PROCESSOR_H
#ifndef LEAVEREQUEST_H #define LEAVEREQUEST_H #include <string> class LeaveRequest{ public: int m_iLeaveDays; std::string m_strName; }; #endif // LEAVEREQUEST_H
main
#include <iostream> using namespace std; #include <AbstractProcessor.h> #include <Leader1Processor.h> #include <Leader2Processor.h> #include <Leader3Processor.h> int main() { LeaveRequest request; Leader1Processor processor1; processor1.handlerName = "Leader1"; Leader2Processor processor2; processor2.handlerName = "Leader2"; Leader3Processor processor3; processor3.handlerName = "Leader3"; processor1.setNextHandler(&processor2); processor2.setNextHandler(&processor3); request.m_iLeaveDays = 20; request.m_strName = "Shiver"; printf("%s request Leave for %d\n",request.m_strName.c_str(),request.m_iLeaveDays); printf("request start\n"); processor1.handlerRequest(&request); printf("request end\n"); return 0; }
总结
1、责任链模式使用场景
相对于原数据进行处理比较好,设定总体处理流程。
2、责任链模式与装饰器模式
装饰器模式更像是加上额外功能,每次处理过后原数据都需要转成另一种数据(类)。