-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchain-of-responsability.cpp
More file actions
168 lines (140 loc) · 3.68 KB
/
chain-of-responsability.cpp
File metadata and controls
168 lines (140 loc) · 3.68 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*!
* This sample is inspired by another one found here :
* https://www.bogotobogo.com/DesignPatterns/chain_of_responsibility.php
*/
#include <iostream>
#include <string>
// Scale of the photo
enum SCALE { S50, S100, S200, S300, S500 };
// Priority of a photo to be treated
enum PRIO { NO_PRIO, MEDIUM_PRIO, HIGH_PRIO };
/*!
* @brief : Request class representation
*/
class Photo
{
public:
Photo(std::string p_title, PRIO p_prio = NO_PRIO) :
m_title(p_title), m_curScale(S100), m_prio(p_prio) {}
void setScale(const SCALE& p_scale) { m_curScale = p_scale; }
SCALE getScale(void) const { return m_curScale; }
PRIO getPrio(void) const { return m_prio; }
private:
std::string m_title;
SCALE m_curScale;
PRIO m_prio;
};
/*!
* @brief : Handler interface.
* - Provides the handle method default behaviour
* * Handle the Photo
* * Forward it to the successor if any
* - Also provide implementation for adding a
* successor.
*/
class PhotoProcessor
{
public:
PhotoProcessor() : m_next(nullptr) { }
public:
virtual void handle( Photo &p ) {
handleImpl(p);
if ( m_next ) { m_next->handle(p); }
}
virtual ~PhotoProcessor() = default;
void setNext(PhotoProcessor* p_next) {
if ( m_next ) { m_next->setNext(p_next); }
else { m_next = p_next; }
}
protected:
virtual void handleImpl(Photo &p) = 0;
PhotoProcessor* m_next;
};
/*!
* @brief : ConcreteHandler.
* Implements Handler's interface.
*/
class Scale : public PhotoProcessor
{
public:
Scale(SCALE p_scale) : m_scale(p_scale) { }
private:
void handleImpl(Photo &a) override {
std::cout << "Scaling photo\n";
a.setScale(m_scale);
}
SCALE m_scale;
};
class PriorityChecker : public PhotoProcessor
{
public:
PriorityChecker(PRIO p_prio) : m_prio(p_prio) { }
/*!
* This ConcreteChecker overrides the handle
* method to define a new behaviour :
* - Handle and perform the next operation
* only if the request's priority is high
* enough.
*/
void handle( Photo &p ) override {
if ( m_prio <= p.getPrio() )
{
handleImpl(p);
if ( m_next ) { m_next->handle(p); }
}
else
{
std::cout << "This Photo is not a priority and will not be treated now\n";
}
}
private:
void handleImpl(Photo &a) override {
(void)a;
std::cout << "Checked photo priority\n";
}
PRIO m_prio;
};
class RedEye : public PhotoProcessor
{
private:
void handleImpl(Photo &a) override { (void)a; std::cout << "Removing red eye\n"; }
};
class Filter : public PhotoProcessor
{
private:
void handleImpl(Photo &a) override { (void)a; std::cout << "Applying filters\n"; }
};
class ColorMatch : public PhotoProcessor
{
private:
void handleImpl(Photo &a) override { (void)a; std::cout << "Matching colors\n"; }
};
/*!
* @brief : Client code.
* Declare the ConcreteHandlers and
* combine them to perform sequential
* operations on the Request.
*/
void processPhoto( Photo& photo )
{
// Declare the ConcreteHandlers
ColorMatch match;
RedEye eye;
Filter filter;
PriorityChecker prioCheck(HIGH_PRIO);
// Build the chain of responsability
// i.e. the order of the operations
Scale CoR(S200);
CoR.setNext (&prioCheck);
CoR.setNext (&eye );
CoR.setNext (&match );
CoR.setNext (&filter );
// Perform the operations
CoR.handle(photo);
}
int main()
{
Photo p("Y2013 Photo");
processPhoto(p);
return 0;
}