职责链模式(Chain of Responsibility Pattern):避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这类对象链接成一条链,并沿着这条链传递请求,直到有对象处理它为止。
(1)Hander(抽象处理者)
定义了一个处理请求的接口;
(2)ConcreteHander(具体处理者)
处理它所负责的请求,如果可处理该请求,就处理掉这个请求;否则将该请求转发给它的下一个可以处理该请求的对象,所以它必须能访问它的下一个可以处理同样请求的对象;
(3)Client(客户类):
向处理对象提出具体的请求。
优点:
缺点:
请假审批系统实现。
- #include
- using namespace std;
- #define SAFE_DELETE(p) if (p) { delete p; p = NULL; }
-
- //请求类 封装请求相关的信息
- class HolidayRequest
- {
- public:
- HolidayRequest(string leaveName, int leaveDays):m_leaveName(leaveName), m_leaveDays(leaveDays){};
- void setLeaveName(string leaveName) {
- m_leaveName = leaveName;
- }
- string getLeaveName(){
- return m_leaveName;
- }
-
- void setLeaveDays(int leaveDays){
- m_leaveDays = leaveDays;
- }
- int getLeaveDays(){
- return m_leaveDays;
- }
- private:
- string m_leaveName;
- int m_leaveDays;
- };
-
- //抽象处理者类 The holiday request handler interface
- class Manager
- {
- public:
- //抽象请求处理方法
- virtual bool HandleRequest(HolidayRequest *pRequest) = 0;
- };
-
- // 具体处理者 Project manager
- class PM : public Manager
- {
- public:
- PM(Manager *handler):m_pHandler(handler){};
- bool HandleRequest(HolidayRequest *pRequest)
- {
- if (pRequest->getLeaveDays() <= 3)
- {
- cout<<"PM said:OK."<
- cout << "leave name is " << pRequest->getLeaveName() << " and leave days is " << pRequest->getLeaveDays();
- return true;
- }
- else{
- if(m_pHandler != NULL){
- return m_pHandler->HandleRequest(pRequest);
- }
-
- }
- return false;
- }
- private:
- Manager *m_pHandler;
- };
-
-
- // 具体处理者 Department manager
- class DM : public Manager
- {
- public:
- DM(Manager *handler) : m_pHandler(handler){}
- bool HandleRequest(HolidayRequest *pRequest)
- {
- if (pRequest->getLeaveDays() <= 10){
- cout<<"DM said:OK."<
- cout << "leave name is " << pRequest->getLeaveName() << " and leave days is " << pRequest->getLeaveDays();
- return true;
- }
- else{
- if(m_pHandler != NULL){
- return m_pHandler->HandleRequest(pRequest);
- }
- }
- return false;
- }
- }
- private:
- Manager *m_pHandler;
- };
-
- //具体处理者 Project supervisor
- class PS : public Manager
- {
- public:
- PS(Manager *handler) : m_pHandler(handler){}
- bool HandleRequest(HolidayRequest *pRequest)
- {
- if (pRequest->getLeaveDays() <= 30){
- cout<<"DM said:OK."<
- cout << "leave name is " << pRequest->getLeaveName() << " and leave days is " << pRequest->getLeaveDays()<
- return true;
- }
- else{
- cout << "Exceed the maximum number of days off" ;
- return false;
- }
- return false;
- }
- private:
- Manager *m_pHandler;
- };
-
- int main()
- {
- //职责链创建
- DM *pDM = new DM(NULL);
- PS *pPS = new PS(pDM);
- PM *pPM = new PM(pPS);
- HolidayRequest *pHolidayRequest = new HolidayRequest("xiaoming",1);
-
- pPM->HandleRequest(pHolidayRequest);
- SAFE_DELETE(pHolidayRequest);
- pHolidayRequest = new HolidayRequest("xiaogang",5);
- pPM->HandleRequest(pHolidayRequest);
-
- SAFE_DELETE(pDM);
- SAFE_DELETE(pPS);
- SAFE_DELETE(pPM);
- SAFE_DELETE(pHolidayRequest);
- }
-
PM said:OK.
leave name is xiaoming and leave days is 1DM said:OK.
leave name is xiaogang and leave days is 5
参考文献:
-
相关阅读:
PHY6230低成本遥控灯控芯片国产蓝牙BLE5.2 2.4G SoC
后端各种格式传参混合vue前端传参
深入理解右值引用与移动语义
电脑系统重装下载的系统在哪找到
C++的string你可能会用,但是你模拟实现过了吗?带你实现以下string的重要接口!
从零开始学数据结构系列之第三章《二叉排序树基础了解》
xss.pwnfunction.com靶机 Warmups
UE AI里的感知实现流程解析
二叉树的经典OJ题
Linux网络技术学习(四)—— 用户空间与内核的接口
-
原文地址:https://blog.csdn.net/kenjianqi1647/article/details/127870557