-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample02.cpp
More file actions
192 lines (149 loc) · 5.63 KB
/
Copy pathConceptualExample02.cpp
File metadata and controls
192 lines (149 loc) · 5.63 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
// ===========================================================================
// ConceptualExample02.cpp // Observer // Variant 2
// ===========================================================================
/**
* Observer Design Pattern
*/
#include <list>
#include <map>
#include <memory>
#include <print>
#include <string>
namespace ObserverDesignPatternSmartPointer {
class IObserver {
public:
virtual ~IObserver() {}
virtual void update(const std::string&) = 0;
};
class ISubject {
public:
virtual ~ISubject() {}
virtual void attach(std::weak_ptr<IObserver>) = 0;
virtual void detach(std::weak_ptr<IObserver>) = 0;
};
// =======================================================================
/**
* The Subject owns some important state and notifies observers
* when the state changes.
*/
class Subject : public ISubject {
private:
std::list<std::weak_ptr<IObserver>> m_observers;
std::string m_message;
public:
virtual ~Subject() noexcept {
std::println("d'tor Subject");
}
/**
* subscription management methods.
*/
void attach(std::weak_ptr<IObserver> observer) override {
m_observers.push_back(observer);
}
void detach(std::weak_ptr<IObserver> observer) override {
// https://stackoverflow.com/questions/10120623/removing-item-from-list-of-weak-ptrs
m_observers.remove_if([observer](std::weak_ptr<IObserver> wp) {
return !observer.owner_before(wp) && !wp.owner_before(observer);
}
);
m_observers.remove_if([observer](const std::weak_ptr<IObserver>& wp) {
return !observer.owner_before(wp) && !wp.owner_before(observer);
}
);
}
void createMessage(const std::string& message) {
m_message = message;
notify();
}
/**
* Usually, the subscription logic is only a fraction of what a Subject can
* really do. Subjects commonly hold some important business logic, that
* triggers a notification method whenever something important is about to
* happen (or after it).
*/
void someBusinessLogic() {
std::println("Subject: changing state ...");
m_message = "changing this message";
notify();
}
private:
void notify() {
// clean up expired weak pointers while notifying active observers
m_observers.remove_if([](const std::weak_ptr<IObserver>& wp) {
return wp.expired();
}
);
for (const std::weak_ptr<IObserver>& weakPtr : m_observers) {
std::shared_ptr<IObserver> sharedPtr{ weakPtr.lock() };
if (sharedPtr != nullptr) {
sharedPtr->update(m_message);
}
}
}
};
// ===========================================================================
class Observer : public IObserver {
private:
std::string m_messageFromSubject;
std::size_t m_number;
public:
Observer()
{
static std::size_t nextNumber = 0;
m_number = nextNumber++;
std::println("Observer: {}", m_number);
}
virtual ~Observer() noexcept
{
std::println("d'tor Observer ({})", m_number);
}
void update(const std::string& messageFromSubject) override
{
m_messageFromSubject = messageFromSubject;
printInfo();
}
void printInfo() const {
std::println("Observer: new message is available --> \"{}\"", m_messageFromSubject);
}
};
static void clientCode_01() {
std::shared_ptr<Subject> subject{ std::make_shared<Subject>() };
std::shared_ptr<IObserver> observer1{ std::make_shared<Observer>() };
std::shared_ptr<IObserver> observer2{ std::make_shared<Observer>() };
std::shared_ptr<IObserver> observer3{ std::make_shared<Observer>() };
subject->attach(observer1);
subject->attach(observer2);
subject->attach(observer3);
subject->createMessage("Hello World!");
subject->createMessage("Hello World Again");
subject->detach(observer1);
subject->detach(observer2);
subject->detach(observer3);
}
static void clientCode_02() {
std::shared_ptr<Subject> subject{ std::make_shared<Subject>() };
std::shared_ptr<IObserver> observer1{ std::make_shared<Observer>() };
std::shared_ptr<IObserver> observer2{ std::make_shared<Observer>() };
subject->attach(observer1);
subject->attach(observer2);
{
std::shared_ptr<IObserver> observer3{ std::make_shared<Observer>() };
subject->attach(observer3);
subject->createMessage("Hello World!");
}
// Note: Watch contents of 'm_observers' list
// Expired weak_ptr will be cleaned up on next notify()
subject->createMessage("Hello World Again");
subject->detach(observer1);
subject->detach(observer2);
}
}
// ===========================================================================
void test_conceptual_example_02()
{
ObserverDesignPatternSmartPointer::clientCode_01();
ObserverDesignPatternSmartPointer::clientCode_02();
}
// ===========================================================================
// End-of-File
// ===========================================================================