design pattern CHAIN of reponsibility

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<cstdio>
using namespace std;
namespace pattern{
/// this pattern links all handler as a line, if previous handler can process a request, it wil not be
/// send to next handler. For example, if someone requested changing programming style,
/// this decision will be made by a programmer.
/// He will change his programming style without noticing his lead.
/// But if someone request changing the requirements, the programmer can not make this decision.
/// He will noticing his lead, and make his lead decided.
/// this is request class.
#define STYLE 0
#define REQIREMENT 1
class Request{
public:
int type; // changing programming style or changing requirements?
};
/// this class for processing request.
class Handler{
private:
Handler* parent;
public:
Handler(Handler* p):parent(p){}
virtual bool DoHandle(Request& r)=0;
void Process(Request& r);
virtual ~Handler(){}
};
void Handler::Process(Request& r){
if(!this->DoHandle(r) && NULL != parent){
parent->Process(r);
}
}
class Programmer: public Handler{
public:
Programmer(Handler* p):Handler(p){}
virtual bool DoHandle(Request& r);
};
bool Programmer::DoHandle(Request& r){
if(r.type == STYLE){
printf("programmer: I can process changing programming style\n");
return true;
}else{
printf("programmer: I can not process this request. I will report this to my lead!\n");
return false;
}
}
class Lead:public Handler{
public:
Lead(Handler* p):Handler(p){}
virtual bool DoHandle(Request& r);
};
bool Lead::DoHandle(Request& r){
if(r.type == REQIREMENT){
printf("lead: I will process changing reqirements\n");
return true;
}
return false;
}
}
int main(){
pattern::Lead lead((pattern::Handler*)NULL);
pattern::Programmer programmer(&lead);
pattern::Request r;
r.type = STYLE;
programmer.Process(r);
r.type = REQIREMENT;
programmer.Process(r);
return 0;
}