职责链模式(Chain of Responsibility):
使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
行为型
UML 类图:
代码实现 C++:
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(const string& s) : name(s), superior(0) {}
20 void SetSuperior(Manager* m)
21 {
22 superior = m;
23 }
24 virtual void RequestApplications(Request* request) = 0;
25 };
26
27 class CommonManager : public Manager
28 {
29 public:
30 CommonManager(const string& s) : Manager(s) {}
31 virtual void RequestApplications(Request* request)
32 {
33 if (request->requestType == "请假" && request->number <= 2)
34 {
35 cout << name << ": " << request->requestContent << " 数量 " << request->number << " 被批准!" << endl;
36 }
37 else if (superior != 0)
38 {
39 superior->RequestApplications(request);
40 }
41 else
42 {
43 cout << "不能得到处理!" << endl;
44 }
45 }
46 };
47
48 class Majordomo : public Manager
49 {
50 public:
51 Majordomo(const string& s) : Manager(s) {}
52 virtual void RequestApplications(Request* request)
53 {
54 if (request->requestType == "请假" && request->number <= 5)
55 {
56 cout << name << ": " << request->requestContent << " 数量 " << request->number << " 被批准!" << endl;
57 }
58 else if (superior != 0)
59 {
60 superior->RequestApplications(request);
61 }
62 else
63 {
64 cout << "不能得到处理!" << endl;
65 }
66 }
67 };
68
69 class GeneralManager : public Manager
70 {
71 public:
72 GeneralManager(const string& s) : Manager(s) {}
73 virtual void RequestApplications(Request* request)
74 {
75 if (request->requestType == "请假")
76 {
77 cout << name << ": " << request->requestContent << " 数量 " << request->number << " 被批准!" << endl;
78 }
79 else if (request->requestType == "加薪" && request->number <= 500)
80 {
81 cout << name << ": " << request->requestContent << " 数量 " << request->number << " 被批准!" << endl;
82 }
83 else if (request->requestType == "加薪" && request->number > 500)
84 {
85 cout << name << ": " << request->requestContent << " 数量 " << request->number << " 再说吧!" << endl;
86 }
87 else if (superior != 0)
88 {
89 superior->RequestApplications(request);
90 }
91 else
92 {
93 cout << "不能得到处理!" << endl;
94 }
95 }
96 };
97
98 int main()
99 {
100 CommonManager* jinli = new CommonManager("金利");
101 Majordomo* zongjian = new Majordomo("宗剑");
102 GeneralManager* zhongjingli = new GeneralManager("钟精励");
103
104 jinli->SetSuperior(zongjian);
105 zongjian->SetSuperior(zhongjingli);
106
107 Request* request = new Request;
108 request->requestType = "请假";
109 request->requestContent = "小菜请假";
110 request->number = 1;
111 jinli->RequestApplications(request);
112
113 Request* request2 = new Request;
114 request2->requestType = "请假";
115 request2->requestContent = "小菜请假";
116 request2->number = 4;
117 jinli->RequestApplications(request2);
118
119 Request* request3 = new Request;
120 request3->requestType = "加薪";
121 request3->requestContent = "小菜请求加薪";
122 request3->number = 500;
123 jinli->RequestApplications(request3);
124
125 Request* request4 = new Request;
126 request4->requestType = "加薪";
127 request4->requestContent = "小菜请求加薪";
128 request4->number = 1000;
129 jinli->RequestApplications(request4);
130
131 return 0;
132 }
posted on 2011-04-30 11:07
unixfy 阅读(182)
评论(0) 编辑 收藏 引用