020 --- 第24章 职责链模式

简述:

  职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。

  职责链模式包括:处理请示接口类、具体处理者类。

    处理请示接口类:定义处理请求的接口。

    具体处理者类:继承自处理请示接口类,处理它所负责的请求,可访问它的后继者,如果可处理该请求,就处理之,否则就将该请求转发给它的后继者。

职责链模式:

 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 using namespace std;
  3 
  4 // 请求类
  5 class CRequest
  6 {
  7 private:
  8     string m_szRequestType; // 申请类别
  9     string m_szRequestContent; // 申请内容
 10     int m_nNumber; // 数量
 11 
 12 public:
 13     void SetRequestType(string szRequestType) { m_szRequestType = szRequestType; }
 14 
 15     string GetRequestType() { return m_szRequestType; }
 16 
 17     void SetRequestContent(string szRequestContent) { m_szRequestContent = szRequestContent; }
 18 
 19     string GetRequestContent() { return m_szRequestContent; }
 20 
 21     void SetNumber(int nNumber) { m_nNumber = nNumber; }
 22 
 23     int GetNumber() { return m_nNumber; }
 24 };
 25 
 26 // 管理者(处理请示接口类)
 27 class CManager
 28 {
 29 protected:
 30     string m_szName;
 31     CManager* m_pSuperior; // 管理者上级
 32 
 33 public:
 34     CManager(string szName) { m_szName = szName; }
 35 
 36     // 设置管理者上级
 37     void SetSuperior(CManager* pSuperior)     { m_pSuperior = pSuperior; }
 38 
 39     // 处理请求
 40     virtual void RequestApplications(CRequest* pRequest) = 0;
 41 };
 42 
 43 // 经理(具体处理类)
 44 class CCommonManager : public CManager
 45 {
 46 public:
 47     CCommonManager(string name) : CManager(name) {}
 48 
 49     virtual void RequestApplications(CRequest* pRequest)
 50     {
 51         if (pRequest->GetRequestType() == "请假" && pRequest->GetNumber() <= 2)
 52             cout << m_szName << ":" << pRequest->GetRequestContent() << " 数量 " << pRequest->GetNumber() << " 被批准" << endl;
 53         else
 54         {
 55             if (m_pSuperior != NULL)
 56                 m_pSuperior->RequestApplications(pRequest);
 57         }
 58     }
 59 };
 60 
 61 // 总监(具体处理类)
 62 class CMajordomo: public CManager
 63 {
 64 public:
 65     CMajordomo(string name) : CManager(name) {}
 66 
 67     virtual void RequestApplications(CRequest* pRequest)
 68     {
 69         if (pRequest->GetRequestType() == "请假" && pRequest->GetNumber() <= 5)
 70             cout << m_szName << ":" << pRequest->GetRequestContent() << " 数量 " << pRequest->GetNumber() << " 被批准" << endl;
 71         else
 72         {
 73             if (m_pSuperior != NULL)
 74                 m_pSuperior->RequestApplications(pRequest);
 75         }
 76     }
 77 };
 78 
 79 // 总经理(具体处理类)
 80 class CGeneralManager : public CManager
 81 {
 82 public:
 83     CGeneralManager(string name) : CManager(name) {}
 84 
 85     virtual void RequestApplications(CRequest* pRequest)
 86     {
 87         if (pRequest->GetRequestType() == "请假")
 88             cout << m_szName << ":" << pRequest->GetRequestContent() << " 数量 " << pRequest->GetNumber() << " 被批准" << endl;
 89         else if (pRequest->GetRequestType() == "加薪" && pRequest->GetNumber() <= 500)
 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     }
 94 };
 95 
 96 int main()
 97 {
 98     CCommonManager JinLi("金利");
 99     CMajordomo ZongJian("宗剑");
100     CGeneralManager ZhongJingLi("钟精励");
101     JinLi.SetSuperior(&ZongJian);
102     ZongJian.SetSuperior(&ZhongJingLi);
103 
104     CRequest Request;
105     Request.SetRequestType("请假");
106     Request.SetRequestContent("GHL请假");
107     Request.SetNumber(1);
108     JinLi.RequestApplications(&Request);
109 
110     CRequest Request2;
111     Request2.SetRequestType("请假");
112     Request2.SetRequestContent("GHL请假");
113     Request2.SetNumber(4);
114     JinLi.RequestApplications(&Request2);
115 
116     CRequest Request3;
117     Request3.SetRequestType("加薪");
118     Request3.SetRequestContent("GHL请求加薪");
119     Request3.SetNumber(500);
120     JinLi.RequestApplications(&Request3);
121 
122     CRequest Request4;
123     Request4.SetRequestType("加薪");
124     Request4.SetRequestContent("GHL请求加薪");
125     Request4.SetNumber(1000);
126     JinLi.RequestApplications(&Request4);
127 
128     system("pause");
129     return 0;
130 }

输出结果:

原文地址:https://www.cnblogs.com/SmallAndGreat/p/13649933.html