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

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

Public Member Functions

void Update ()
 Update the number then notify all observers.
 
void SubscribeToNumberChanged (IObserverNumberChanged observer)
 
void UnsubscribeFromNumberChanged (IObserverNumberChanged observer)
 
uint FetchNumber ()
 Return the current value from the Subject.
 

Private Member Functions

void _NotifyNumberChanged ()
 Helper method to notify all observers that the number has changed.
 
uint INumberProducer. FetchNumber ()
 Observers call this method to fetch the current number.
 
void IEventNotifications. SubscribeToNumberChanged (IObserverNumberChanged 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 IEventNotifications. UnsubscribeFromNumberChanged (IObserverNumberChanged 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.
 

Private Attributes

List< IObserverNumberChanged_observers = new List<IObserverNumberChanged>()
 The list of observers subscribed to this class instance.
 
uint _number
 The number being maintained.
 

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 105 of file ObserverSubject_NumberProducer.cs.

Member Function Documentation

◆ _NotifyNumberChanged()

void _NotifyNumberChanged ( )
inlineprivate

Helper method to notify all observers that the number has changed.

Definition at line 121 of file ObserverSubject_NumberProducer.cs.

References ObserverSubject_NumberProducer._observers, and IObserverNumberChanged.NumberChanged().

Referenced by ObserverSubject_NumberProducer.Update().

◆ FetchNumber()

uint INumberProducer. FetchNumber ( )
inlineprivate

Observers call this method to fetch the current number.

Returns
Returns the current number.

Implements INumberProducer.

Definition at line 156 of file ObserverSubject_NumberProducer.cs.

References ObserverSubject_NumberProducer._number.

◆ SubscribeToNumberChanged()

void IEventNotifications. SubscribeToNumberChanged ( IObserverNumberChanged  observer)
inlineprivate

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.cs for an example of such a lock.

Implements IEventNotifications.

Definition at line 178 of file ObserverSubject_NumberProducer.cs.

References ObserverSubject_NumberProducer._observers.

◆ UnsubscribeFromNumberChanged()

void IEventNotifications. UnsubscribeFromNumberChanged ( IObserverNumberChanged  observer)
inlineprivate

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 is needed. See the HandlerChain.SendMessage() method in HandlerChain_Class.cs for an example of such a lock.

Implements IEventNotifications.

Definition at line 198 of file ObserverSubject_NumberProducer.cs.

References ObserverSubject_NumberProducer._observers.

◆ Update()

void Update ( )
inline

Update the number then notify all observers.

Definition at line 141 of file ObserverSubject_NumberProducer.cs.

References ObserverSubject_NumberProducer._NotifyNumberChanged(), and ObserverSubject_NumberProducer._number.

Referenced by Observer_Exercise.Run().

Member Data Documentation

◆ _number

uint _number
private

The number being maintained.

Definition at line 115 of file ObserverSubject_NumberProducer.cs.

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

◆ _observers


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