Design Pattern Examples
Overview of object-oriented design patterns
ObserverSubject_NumberProducer Class Reference

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>

Inheritance diagram for ObserverSubject_NumberProducer:
Inheritance graph
Collaboration diagram for ObserverSubject_NumberProducer:
Collaboration graph

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.
 

Detailed Description

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.

Member Typedef Documentation

◆ ObserversList

Definition at line 147 of file ObserverSubject_NumberProducer.h.

Constructor & Destructor Documentation

◆ ObserverSubject_NumberProducer()

Default constructor.

Definition at line 212 of file ObserverSubject_NumberProducer.h.

Member Function Documentation

◆ _ContainsObserver()

bool _ContainsObserver ( const IObserverNumberChanged::shared_ptr_t observer)
inlineprivate

Helper method to determine if the specified observer is already present in the list of observers for this class.

Parameters
observerAn observer to look for.
Returns
Returns true if the observer is on this class's list of observers; otherwise, returns false.

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().

◆ _FindObserver()

ObserversList::iterator _FindObserver ( const IObserverNumberChanged::shared_ptr_t observer)
inlineprivate

Helper method to retrieve the iterator to the specified observer if the observer is in the list.

Parameters
observerAn observer to look for.
Returns
Returns an iterator to the found observer; otherwise, returns 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().

◆ _NotifyNumberChanged()

void _NotifyNumberChanged ( )
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().

◆ FetchNumber()

uint32_t FetchNumber ( )
inlineoverridevirtual

Observers call this method to fetch the current number.

Returns
Returns the current number.

Implements INumberProducer.

Definition at line 236 of file ObserverSubject_NumberProducer.h.

References ObserverSubject_NumberProducer::_number.

◆ SubscribeToNumberChanged()

void SubscribeToNumberChanged ( IObserverNumberChanged::shared_ptr_t  observer)
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.

Parameters
observerAn 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.

◆ UnsubscribeFromNumberChanged()

void UnsubscribeFromNumberChanged ( IObserverNumberChanged::shared_ptr_t  observer)
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.

Parameters
observerAn 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.

◆ Update()

void Update ( )
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.

Member Data Documentation

◆ _number

uint32_t _number
private

The number being maintained.

Definition at line 157 of file ObserverSubject_NumberProducer.h.

Referenced by ObserverSubject_NumberProducer::FetchNumber(), and ObserverSubject_NumberProducer::Update().

◆ _observers


The documentation for this class was generated from the following file: