-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchainofresposibility.cpp
120 lines (107 loc) · 2.29 KB
/
chainofresposibility.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//通过将多个处理对象组成一条链,每个处理对象都有机会处理请求。
//如果某个对象不能处理请求,则将请求传递给下一个对象依次处理,直到请求被处理为止。
#include <iostream>
#include <string>
enum RequestType
{
REQ_HANDLER1,
REQ_HANDLER2,
REQ_HANDLER3
};
class Request
{
std::string description;
RequestType reqType;
public:
Request(const std::string& desc, RequestType type) : description(desc), reqType(type)
{
}
RequestType GetReqType() const
{
return reqType;
}
const std::string& GetDescription() const
{
return description;
}
};
class ChainHandler
{
ChainHandler* nextChain;
void SendRequestToNextHandler(const Request& req)
{
if (nextChain)
{
nextChain->Handle(req);
}
}
protected:
virtual bool CanHandleRequest(const Request& req) = 0;
virtual void ProcessRequest(const Request& req) = 0;
public:
ChainHandler() : nextChain(nullptr)
{
}
void SetNextChain(ChainHandler* next)
{
nextChain = next;
}
void Handle(const Request& req)
{
if (CanHandleRequest(req))
{
ProcessRequest(req);
}
else
{
SendRequestToNextHandler(req);
}
}
};
class Handler1 : public ChainHandler
{
protected:
bool CanHandleRequest(const Request& req) override
{
return req.GetReqType() == RequestType::REQ_HANDLER1;
}
void ProcessRequest(const Request& req) override
{
std::cout << "Handler1 is handle request:" << req.GetDescription() << std::endl;
}
};
class Handler2 : public ChainHandler
{
protected:
bool CanHandleRequest(const Request& req) override
{
return req.GetReqType() == RequestType::REQ_HANDLER2;
}
void ProcessRequest(const Request& req) override
{
std::cout << "Handler2 is handle request:" << req.GetDescription() << std::endl;
}
};
class Handler3 : public ChainHandler
{
protected:
bool CanHandleRequest(const Request& req) override
{
return req.GetReqType() == RequestType::REQ_HANDLER3;
}
void ProcessRequest(const Request& req) override
{
std::cout << "Handler2 is handle request:" << req.GetDescription() << std::endl;
}
};
//int main()
//{
// Handler1 h1;
// Handler2 h2;
// Handler3 h3;
// h1.SetNextChain(&h2);
// h2.SetNextChain(&h3);
//
// Request req("process task ", RequestType::REQ_HANDLER3);
// h1.Handle(req);
//}