-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample.cpp
More file actions
223 lines (180 loc) · 5.57 KB
/
Copy pathConceptualExample.cpp
File metadata and controls
223 lines (180 loc) · 5.57 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// ===========================================================================
// ConceptualExample.cpp // MVC
// ===========================================================================
#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
#include <iostream>
#include <list>
#include <memory>
#include <string>
namespace ConceptualExample {
// interface observer
struct IValveObserver
{
virtual ~IValveObserver() = default;
virtual void valveValueChanged(int) = 0;
};
// interface valve model
struct IValveModel
{
// getter/setter
virtual int getMinimum() = 0;
virtual int getMaximum() = 0;
// methods
virtual void widenOpening() = 0;
virtual void narrowOpening() = 0;
// observer functionality
virtual void attach(const std::shared_ptr<IValveObserver>&) = 0;
virtual void detach(const std::shared_ptr<IValveObserver>&) = 0;
};
// implementation of model
class ValveModelImpl : public IValveModel
{
private:
// "openness" of the valve is limited (range 0 to 100)
const int Minimum = 0;
const int Maximum = 100;
// valve state
int m_value;
// list of observers
std::list<std::weak_ptr<IValveObserver>> m_observers;
public:
ValveModelImpl()
{
m_value = (getMaximum() - getMinimum()) / 2;
}
int getMinimum() override
{
return Minimum;
}
int getMaximum() override
{
return Maximum;
}
void widenOpening() override
{
if (m_value < getMaximum()) {
m_value++;
onValveValueChanged(m_value);
}
}
void narrowOpening() override
{
if (m_value > getMinimum()) {
m_value--;
onValveValueChanged(m_value);
}
}
// subscription management methods
void attach(const std::shared_ptr<IValveObserver>& observer) override {
m_observers.push_back(observer);
}
void detach(const std::shared_ptr<IValveObserver>& observer) override {
// https://stackoverflow.com/questions/10120623/removing-item-from-list-of-weak-ptrs
m_observers.remove_if([&](std::weak_ptr<IValveObserver> wp) {
return !observer.owner_before(wp) && !wp.owner_before(observer);
});
}
private:
void onValveValueChanged(int value)
{
for (std::weak_ptr<IValveObserver> observer : m_observers) {
std::shared_ptr<IValveObserver> sp{ observer.lock() };
if (sp != nullptr) {
sp->valveValueChanged(value);
}
}
}
};
// implementation of a view
class ValveViewLabel : public IValveObserver, public std::enable_shared_from_this<ValveViewLabel>
{
public:
ValveViewLabel() {}
void attach(std::shared_ptr<IValveModel> model) {
model->attach(shared_from_this());
}
void valveValueChanged(int value) override
{
std::cout << "Label: " << value << std::endl;
}
};
// implementation of two controllers
class OpenValveController
{
private:
std::shared_ptr<IValveModel> m_model;
public:
OpenValveController(std::shared_ptr<IValveModel> model)
{
m_model = model;
}
void onClick()
{
m_model->widenOpening();
}
};
class CloseValveController
{
private:
std::shared_ptr<IValveModel> m_model;
public:
CloseValveController(std::shared_ptr<IValveModel> model)
{
m_model = model;
}
void onClick()
{
m_model->narrowOpening();
}
};
static void clientCode()
{
// create model
std::shared_ptr<IValveModel> model{ std::make_shared<ValveModelImpl>() };
// create view (being connected to the model)
std::shared_ptr<ValveViewLabel> labelView{
std::make_shared<ValveViewLabel>()
};
labelView->attach(model);
// create controller (being connected to the model)
std::shared_ptr<OpenValveController> openController{
std::make_shared<OpenValveController>(model)
};
// create second controller (being connected to the model)
std::shared_ptr<CloseValveController> closeController{
std::make_shared<CloseValveController>(model)
};
// wait for input - message loop
char ch{ '?' };
std::cout << "Waiting for input ['+' == open / '-' == close / 'q' == quit]" << std::endl;
while (ch != 'q') {
std::cin >> ch;
if (ch == '+') {
openController->onClick();
}
else if (ch == '-') {
closeController->onClick();
}
else if (ch == 'q') {
break;
}
}
model->detach(labelView);
}
}
void test_conceptual_example () {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
using namespace ConceptualExample;
clientCode();
}
// ===========================================================================
// End-of-File
// ===========================================================================