020 --- 第24章 职责链模式
简述:
职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
职责链模式包括:处理请示接口类、具体处理者类。
处理请示接口类:定义处理请求的接口。
具体处理者类:继承自处理请示接口类,处理它所负责的请求,可访问它的后继者,如果可处理该请求,就处理之,否则就将该请求转发给它的后继者。
应用场景:同一类的不同的对象,传入到当前对象作为后继者,如果当前对象不处理逻辑,则传递给后继者继续处理
注:开发环境调整为VS2017,操作系统win11
职责链模式:
1 #include <iostream> 2 using namespace std; 3 4 // 处理请示接口类 5 class CHandler 6 { 7 protected: 8 CHandler* m_pSuccessor; 9 10 public: 11 void SetSuccessor(CHandler* pSuccessor) 12 { 13 m_pSuccessor = pSuccessor; 14 } 15 16 virtual void HandlerRequest(int nRquest) = 0; 17 }; 18 19 // 具体处理者类 20 class CConcreteHandler1 : public CHandler 21 { 22 public: 23 virtual void HandlerRequest(int nRquest) 24 { 25 if (nRquest >= 0 && nRquest < 10) 26 cout << "Handler1 处理请求 " << nRquest << endl; 27 else if (m_pSuccessor != NULL) 28 m_pSuccessor->HandlerRequest(nRquest); 29 } 30 }; 31 32 // 具体处理者类 33 class CConcreteHandler2 : public CHandler 34 { 35 public: 36 virtual void HandlerRequest(int nRquest) 37 { 38 if (nRquest >= 10 && nRquest < 20) 39 cout << "Handler2 处理请求 " << nRquest << endl; 40 else if (m_pSuccessor != NULL) 41 m_pSuccessor->HandlerRequest(nRquest); 42 } 43 }; 44 45 // 具体处理者类 46 class CConcreteHandler3 : public CHandler 47 { 48 public: 49 virtual void HandlerRequest(int nRquest) 50 { 51 if (nRquest >= 20 && nRquest < 30) 52 cout << "Handler3 处理请求 " << nRquest << endl; 53 else if (m_pSuccessor != NULL) 54 m_pSuccessor->HandlerRequest(nRquest); 55 } 56 }; 57 58 int main() 59 { 60 CConcreteHandler1 Handler1; 61 CConcreteHandler2 Handler2; 62 CConcreteHandler3 Handler3; 63 Handler1.SetSuccessor(&Handler2); 64 Handler2.SetSuccessor(&Handler3); 65 66 int requests[] = {2, 5, 14, 22, 18, 3, 27, 20}; 67 68 for (auto request : requests) 69 Handler1.HandlerRequest(request); 70 71 system("pause"); 72 return 0; 73 }
输出结果:
例:员工请求加薪
代码如下:
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 6 // 请求类 7 class CRequest 8 { 9 private: 10 string m_szRequestType; // 申请类别 11 string m_szRequestContent; // 申请内容 12 int m_nNumber; // 数量 13 14 public: 15 void SetRequestType(string szRequestType) { m_szRequestType = szRequestType; } 16 17 string GetRequestType() { return m_szRequestType; } 18 19 void SetRequestContent(string szRequestContent) { m_szRequestContent = szRequestContent; } 20 21 string GetRequestContent() { return m_szRequestContent; } 22 23 void SetNumber(int nNumber) { m_nNumber = nNumber; } 24 25 int GetNumber() { return m_nNumber; } 26 }; 27 28 // 管理者(处理请示接口类) 29 class CManager 30 { 31 protected: 32 string m_szName; 33 CManager* m_pSuperior; // 管理者上级 34 35 public: 36 CManager(string szName) { m_szName = szName; } 37 38 // 设置管理者上级 39 void SetSuperior(CManager* pSuperior) { m_pSuperior = pSuperior; } 40 41 // 处理请求 42 virtual void RequestApplications(CRequest* pRequest) = 0; 43 }; 44 45 // 经理(具体处理类) 46 class CCommonManager : public CManager 47 { 48 public: 49 CCommonManager(string name) : CManager(name) {} 50 51 virtual void RequestApplications(CRequest* pRequest) 52 { 53 if (pRequest->GetRequestType() == "请假" && pRequest->GetNumber() <= 2) 54 cout << m_szName << ":" << pRequest->GetRequestContent() << " 数量 " << pRequest->GetNumber() << " 被批准" << endl; 55 else 56 { 57 if (m_pSuperior != NULL) 58 m_pSuperior->RequestApplications(pRequest); 59 } 60 } 61 }; 62 63 // 总监(具体处理类) 64 class CMajordomo: public CManager 65 { 66 public: 67 CMajordomo(string name) : CManager(name) {} 68 69 virtual void RequestApplications(CRequest* pRequest) 70 { 71 if (pRequest->GetRequestType() == "请假" && pRequest->GetNumber() <= 5) 72 cout << m_szName << ":" << pRequest->GetRequestContent() << " 数量 " << pRequest->GetNumber() << " 被批准" << endl; 73 else 74 { 75 if (m_pSuperior != NULL) 76 m_pSuperior->RequestApplications(pRequest); 77 } 78 } 79 }; 80 81 // 总经理(具体处理类) 82 class CGeneralManager : public CManager 83 { 84 public: 85 CGeneralManager(string name) : CManager(name) {} 86 87 virtual void RequestApplications(CRequest* pRequest) 88 { 89 if (pRequest->GetRequestType() == "请假") 90 cout << m_szName << ":" << pRequest->GetRequestContent() << " 数量 " << pRequest->GetNumber() << " 被批准" << endl; 91 else if (pRequest->GetRequestType() == "加薪" && pRequest->GetNumber() <= 500) 92 cout << m_szName << ":" << pRequest->GetRequestContent() << " 数量 " << pRequest->GetNumber() << " 被批准" << endl; 93 else if (pRequest->GetRequestType() == "加薪" && pRequest->GetNumber() > 500) 94 cout << m_szName << ":" << pRequest->GetRequestContent() << " 数量 " << pRequest->GetNumber() << " 再说吧" << endl; 95 } 96 }; 97 98 int main() 99 { 100 CCommonManager JinLi("金利"); 101 CMajordomo ZongJian("宗剑"); 102 CGeneralManager ZhongJingLi("钟精励"); 103 JinLi.SetSuperior(&ZongJian); 104 ZongJian.SetSuperior(&ZhongJingLi); 105 106 CRequest Request; 107 Request.SetRequestType("请假"); 108 Request.SetRequestContent("GHL请假"); 109 Request.SetNumber(1); 110 JinLi.RequestApplications(&Request); 111 112 CRequest Request2; 113 Request2.SetRequestType("请假"); 114 Request2.SetRequestContent("GHL请假"); 115 Request2.SetNumber(4); 116 JinLi.RequestApplications(&Request2); 117 118 CRequest Request3; 119 Request3.SetRequestType("加薪"); 120 Request3.SetRequestContent("GHL请求加薪"); 121 Request3.SetNumber(500); 122 JinLi.RequestApplications(&Request3); 123 124 CRequest Request4; 125 Request4.SetRequestType("加薪"); 126 Request4.SetRequestContent("GHL请求加薪"); 127 Request4.SetNumber(1000); 128 JinLi.RequestApplications(&Request4); 129 130 system("pause"); 131 return 0; 132 }
输出结果: