Design Pattern Examples
Overview of object-oriented design patterns
ObserverSubject_NumberProducer.h
Go to the documentation of this file.
1
19
20#pragma once
21#ifndef __OBSERVERSUBJECT_NUMBERPRODUCER_H__
22#define __OBSERVERSUBJECT_NUMBERPRODUCER_H__
23
24#include <algorithm>
25#include <memory>
26#include <stdint.h>
27#include <vector>
28
30{
31
42 {
46 using shared_ptr_t = std::shared_ptr<IObserverNumberChanged>;
47
52
57 virtual void NumberChanged() = 0;
58 };
59
60
61 //########################################################################
62 //########################################################################
63
64
80 {
85
88 };
89
90
91 //########################################################################
92 //########################################################################
93
94
108 {
112 using shared_ptr_t = std::shared_ptr<INumberProducer>;
113
117 virtual ~INumberProducer() {}
118
122 virtual void Update() = 0;
123
128 virtual uint32_t FetchNumber() = 0;
129 };
130
131
132 //########################################################################
133 //########################################################################
134
135
144 {
145 private:
146
147 using ObserversList = std::vector<IObserverNumberChanged::shared_ptr_t>;
148
153
157 uint32_t _number;
158
159
160 private:
165 {
166 // Copy the list so observers can change the original observers
167 // during the notification (this isn't strictly needed in this
168 // example but it is good practice for any notification system
169 // that handles multiple observers where multiple threads might
170 // be in play or observers can unsubscribe at any time, even in
171 // the event notification).
172 ObserversList observers = _observers;
173
174 for(IObserverNumberChanged::shared_ptr_t observer : observers)
175 {
176 observer->NumberChanged();
177 }
178 }
179
187 ObserversList::iterator _FindObserver(const IObserverNumberChanged::shared_ptr_t& observer)
188 {
189 ObserversList::iterator foundIter;
190 return std::find(std::begin(_observers), std::end(_observers), observer);
191 }
192
193
202 {
203 ObserversList::iterator foundIter;
204 foundIter = _FindObserver(observer);
205 return foundIter != std::end(_observers);
206 }
207
208 public:
213 : _number(0)
214 {
215 }
216
217
221 void Update() override
222 {
223 ++_number;
225 }
226
227
228 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
229 // Implementation of the INumberProducer interface.
230 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
231
236 uint32_t FetchNumber() override
237 {
238 return _number;
239 }
240
241
242 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
243 // Implementation of the IEventNotifications interface.
244 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
245
259 {
260 if (!_ContainsObserver(observer))
261 {
262 _observers.push_back(observer);
263 }
264 }
265
266
280 {
281 ObserversList::iterator foundIter;
282 foundIter = _FindObserver(observer);
283 if (foundIter != std::end(_observers))
284 {
285 _observers.erase(foundIter);
286 }
287 }
288 };
289
290} // end namespace
291
292
293#endif // __OBSERVERSUBJECT_NUMBERPRODUCER_H__
294
Represents the Subject in this example, in this case, a class that contains a single number that is u...
ObserversList::iterator _FindObserver(const IObserverNumberChanged::shared_ptr_t &observer)
Helper method to retrieve the iterator to the specified observer if the observer is in the list.
ObserversList _observers
The list of observers subscribed to this class instance.
void UnsubscribeFromNumberChanged(IObserverNumberChanged::shared_ptr_t observer)
A client calls this to unsubscribe an observer from this class instance so notifications are no longe...
void SubscribeToNumberChanged(IObserverNumberChanged::shared_ptr_t observer)
A client calls this to subscribe an observer to this class instance for notifications about changing ...
uint32_t FetchNumber() override
Observers call this method to fetch the current number.
void _NotifyNumberChanged()
Helper method to notify all observers that the number has changed.
void Update() override
Update the number then notify all observers.
bool _ContainsObserver(const IObserverNumberChanged::shared_ptr_t &observer)
Helper method to determine if the specified observer is already present in the list of observers for ...
std::vector< IObserverNumberChanged::shared_ptr_t > ObserversList
The namespace containing all Design Pattern Examples implemented in C++.
Represents a Subject that takes observers implementing the IObserverNumberChanged interface.
virtual ~IEventNotifications()
Virtual destructor required for interfaces in abstract classes.
virtual void UnsubscribeFromNumberChanged(IObserverNumberChanged::shared_ptr_t observer)=0
virtual void SubscribeToNumberChanged(IObserverNumberChanged::shared_ptr_t observer)=0
Represents the Subject to the observers. This is the minimum needed by observers to get access to the...
virtual void Update()=0
Update the number then notify all observers.
virtual uint32_t FetchNumber()=0
Return the current value from the Subject.
std::shared_ptr< INumberProducer > shared_ptr_t
Alias to make it easier to use this shared pointer.
virtual ~INumberProducer()
Virtual destructor required for interfaces in abstract classes.
Represents an observer to the Subject class. An observer implements this interface and then subscribe...
virtual void NumberChanged()=0
This is called whenever the number in the ObserverSubject_NumberProducer object is changed.
std::shared_ptr< IObserverNumberChanged > shared_ptr_t
Alias to make it easier to use this shared pointer.
virtual ~IObserverNumberChanged()
Virtual destructor required for interfaces in abstract classes.