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...
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. | |
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.
|
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().
|
inlineprivate |
Observers call this method to fetch the current number.
Implements INumberProducer.
Definition at line 156 of file ObserverSubject_NumberProducer.cs.
References ObserverSubject_NumberProducer._number.
|
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.
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.cs for an example of such a lock.
Implements IEventNotifications.
Definition at line 178 of file ObserverSubject_NumberProducer.cs.
References ObserverSubject_NumberProducer._observers.
|
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.
observer | An 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.
|
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().
|
private |
The number being maintained.
Definition at line 115 of file ObserverSubject_NumberProducer.cs.
Referenced by ObserverSubject_NumberProducer.FetchNumber(), and ObserverSubject_NumberProducer.Update().
|
private |
The list of observers subscribed to this class instance.
Definition at line 110 of file ObserverSubject_NumberProducer.cs.
Referenced by ObserverSubject_NumberProducer._FindObserver(), ObserverSubject_NumberProducer._NotifyNumberChanged(), ObserverSubject_NumberProducer.SubscribeToNumberChanged(), and ObserverSubject_NumberProducer.UnsubscribeFromNumberChanged().