Represents the Subject in this example, in this case, a class that contains a single number that is updated with a call to the Update() method. Whenever Update() is called, the number is incremented and all observers are notified. The observers then fetch the current number via the INumberProducer interface. More...
#include <ObserverSubject_NumberProducer.h>
Public Member Functions | |
ObserverSubject_NumberProducer () | |
Default constructor. | |
void | Update () override |
Update the number then notify all observers. | |
uint32_t | FetchNumber () override |
Observers call this method to fetch the current number. | |
void | SubscribeToNumberChanged (IObserverNumberChanged::shared_ptr_t observer) |
A client calls this to subscribe an observer to this class instance for notifications about changing numbers. Does nothing if the given observer is already subscribed. | |
void | UnsubscribeFromNumberChanged (IObserverNumberChanged::shared_ptr_t observer) |
A client calls this to unsubscribe an observer from this class instance so notifications are no longer received. Does nothing if the given observer was not subscribed. | |
Public Member Functions inherited from IEventNotifications | |
virtual | ~IEventNotifications () |
Virtual destructor required for interfaces in abstract classes. | |
virtual void | SubscribeToNumberChanged (IObserverNumberChanged::shared_ptr_t observer)=0 |
virtual void | UnsubscribeFromNumberChanged (IObserverNumberChanged::shared_ptr_t observer)=0 |
Public Member Functions inherited from INumberProducer | |
virtual | ~INumberProducer () |
Virtual destructor required for interfaces in abstract classes. | |
virtual void | Update ()=0 |
Update the number then notify all observers. | |
virtual uint32_t | FetchNumber ()=0 |
Return the current value from the Subject. | |
Private Types | |
using | ObserversList = std::vector< IObserverNumberChanged::shared_ptr_t > |
Private Member Functions | |
void | _NotifyNumberChanged () |
Helper method to notify all observers that the number has changed. | |
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. | |
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 this class. | |
Private Attributes | |
ObserversList | _observers |
The list of observers subscribed to this class instance. | |
uint32_t | _number |
The number being maintained. | |
Additional Inherited Members | |
Public Types inherited from INumberProducer | |
using | shared_ptr_t = std::shared_ptr< INumberProducer > |
Alias to make it easier to use this shared pointer. | |
Represents the Subject in this example, in this case, a class that contains a single number that is updated with a call to the Update() method. Whenever Update() is called, the number is incremented and all observers are notified. The observers then fetch the current number via the INumberProducer interface.
Definition at line 143 of file ObserverSubject_NumberProducer.h.
|
private |
Definition at line 147 of file ObserverSubject_NumberProducer.h.
|
inline |
Default constructor.
Definition at line 212 of file ObserverSubject_NumberProducer.h.
|
inlineprivate |
Helper method to determine if the specified observer is already present in the list of observers for this class.
observer | An observer to look for. |
Definition at line 201 of file ObserverSubject_NumberProducer.h.
References ObserverSubject_NumberProducer::_FindObserver(), and ObserverSubject_NumberProducer::_observers.
Referenced by ObserverSubject_NumberProducer::SubscribeToNumberChanged(), and ObserverSubject_NumberProducer::UnsubscribeFromNumberChanged().
|
inlineprivate |
Helper method to retrieve the iterator to the specified observer if the observer is in the list.
observer | An observer to look for. |
std::end(_observers)
.Definition at line 187 of file ObserverSubject_NumberProducer.h.
References ObserverSubject_NumberProducer::_observers.
Referenced by ObserverSubject_NumberProducer::_ContainsObserver(), and ObserverSubject_NumberProducer::UnsubscribeFromNumberChanged().
|
inlineprivate |
Helper method to notify all observers that the number has changed.
Definition at line 164 of file ObserverSubject_NumberProducer.h.
References ObserverSubject_NumberProducer::_observers.
Referenced by ObserverSubject_NumberProducer::Update().
|
inlineoverridevirtual |
Observers call this method to fetch the current number.
Implements INumberProducer.
Definition at line 236 of file ObserverSubject_NumberProducer.h.
References ObserverSubject_NumberProducer::_number.
|
inlinevirtual |
A client calls this to subscribe an observer to this class instance for notifications about changing numbers. Does nothing if the given observer is already subscribed.
observer | An observer represented by the IObserverNumberChanged interface. |
In a multi-threaded environment, this method would use a lock of some form. This example doesn't use multiple threads so no lock is needed. See the HandlerChain::SendMessage() method in HandlerChain_Class.cpp for an example of such a lock.
Implements IEventNotifications.
Definition at line 258 of file ObserverSubject_NumberProducer.h.
References ObserverSubject_NumberProducer::_ContainsObserver(), and ObserverSubject_NumberProducer::_observers.
|
inlinevirtual |
A client calls this to unsubscribe an observer from this class instance so notifications are no longer received. Does nothing if the given observer was not subscribed.
observer | An observer represented by the IObserverNumberChanged interface. |
In a multi-threaded environment, this method would use a lock of some form. This example doesn't use multiple threads so no lock is needed. See the HandlerChain::SendMessage() method in HandlerChain_Class.cpp for an example of such a lock.
Implements IEventNotifications.
Definition at line 279 of file ObserverSubject_NumberProducer.h.
References ObserverSubject_NumberProducer::_FindObserver(), and ObserverSubject_NumberProducer::_observers.
|
inlineoverridevirtual |
Update the number then notify all observers.
Implements INumberProducer.
Definition at line 221 of file ObserverSubject_NumberProducer.h.
References ObserverSubject_NumberProducer::_NotifyNumberChanged(), and ObserverSubject_NumberProducer::_number.
|
private |
The number being maintained.
Definition at line 157 of file ObserverSubject_NumberProducer.h.
Referenced by ObserverSubject_NumberProducer::FetchNumber(), and ObserverSubject_NumberProducer::Update().
|
private |
The list of observers subscribed to this class instance.
Definition at line 152 of file ObserverSubject_NumberProducer.h.
Referenced by ObserverSubject_NumberProducer::_ContainsObserver(), ObserverSubject_NumberProducer::_FindObserver(), ObserverSubject_NumberProducer::_NotifyNumberChanged(), ObserverSubject_NumberProducer::SubscribeToNumberChanged(), and ObserverSubject_NumberProducer::UnsubscribeFromNumberChanged().