责任链模式

【1】什么是责任链模式?

责任链模式:

【2】责任链模式的代码示例:

代码示例:

  1 #include<iostream>
  2 #include<string>
  3 using namespace std;
  4 
  5 class Request
  6 {
  7 public:
  8     string requestType;
  9     string requestContent;
 10     int number;
 11 };
 12 
 13 class Manager
 14 {
 15 protected:
 16     string name;
 17     Manager *superior;
 18 public:
 19     Manager(string name)
 20     {
 21         this->name = name;
 22     }
 23     void setSuperior(Manager *superior)
 24     {
 25         this->superior = superior;
 26     }
 27     virtual void requestApplications(Request *request) = 0;
 28 };
 29 
 30 class CommonManager : public Manager
 31 {
 32 public:
 33     CommonManager(string name) : Manager(name)
 34     {}
 35     void requestApplications(Request *request)
 36     {
 37         if (request->requestType == "请假" && request->number <= 2)
 38         {
 39             cout << name << " " << request->requestContent << " 请假天数: " << request->number << " 被批准" << endl;
 40         }
 41         else 
 42         {
 43             if (superior != NULL)
 44             {
 45                 superior->requestApplications(request);
 46             }
 47         }
 48     }
 49 };
 50 
 51 class Majordomo : public Manager
 52 {
 53 public:
 54     Majordomo(string name) : Manager(name)
 55     {}
 56     void requestApplications(Request *request)
 57     {
 58         if (request->requestType == "请假" && request->number <= 5)
 59         {
 60             cout << name << " " << request->requestContent << " 请假天数: " << request->number << " 被批准" << endl;
 61         }
 62         else 
 63         {
 64             if (superior != NULL)
 65             {
 66                 superior->requestApplications(request);
 67             }
 68         }
 69     }
 70 };
 71 
 72 
 73 class GeneralManager : public Manager
 74 {
 75 public:
 76     GeneralManager(string name) : Manager(name)
 77     {}
 78     void requestApplications(Request *request)
 79     {
 80         if (request->requestType == "请假")
 81         {
 82             cout << name << " " << request->requestContent << " 请假天数: " << request->number << " 被批准" << endl;
 83         }
 84     }
 85 };
 86 
 87 int main()
 88 {
 89     CommonManager *jingli = new CommonManager("经理"); 
 90     Majordomo *zongjian = new Majordomo("总监");
 91     GeneralManager *zongjingli = new GeneralManager("总经理");
 92     
 93     jingli->setSuperior(zongjian);
 94     zongjian->setSuperior(zongjingli);
 95     
 96     Request *request = new Request();
 97 
 98     request->requestType = "请假";
 99     request->requestContent = "李俊宏请假";
100     request->number = 1;
101     jingli->requestApplications(request);
102 
103     request->requestType = "请假";
104     request->requestContent = "李俊宏请假";
105     request->number = 4;
106     jingli->requestApplications(request);
107 
108     request->requestType = "请假";
109     request->requestContent = "李俊宏请假";
110     request->number = 10;
111     jingli->requestApplications(request);
112 
113     return 0;
114 }
115 //Result:
116 /*
117 经理 李俊宏请假 请假天数: 1 被批准
118 总监 李俊宏请假 请假天数: 4 被批准
119 总经理 李俊宏请假 请假天数: 10 被批准
120 */
View Code

 

Good   Good   Study, Day   Day  Up.

顺序  选择   循环  总结

posted @ 2014-09-04 19:57  kaizenly  阅读(347)  评论(0编辑  收藏  举报
打赏