Example of using the Observer Pattern in C#. More...
Public Member Functions | |
void | Run () |
Executes the example for the Observer Pattern in C#. | |
Example of using the Observer Pattern in C#.
The Observer pattern allows for one or more observers to react to changes in a Subject entity.
In this exercise, a number producer (the Subject) updates an internal value every time the Update() method is called. Three different observers are attached to the number producer and print out the current value in different formats whenever the number is changed.
Note: Interfaces are used throughout this example. For example, to subscribe to the number producer, the IEventNotifications interface must be obtained from the number producer. The number producer is represented to the observers with the INumberProducer interface and the observers are represented to the number producer with the IObserverNumberChanged interface. This highlights a common way to implement a "pull" style observer without having too much knowledge about the Subject.
Definition at line 30 of file Observer_Exercise.cs.
|
inline |
Executes the example for the Observer Pattern in C#.
Definition at line 36 of file Observer_Exercise.cs.
References IEventNotifications.SubscribeToNumberChanged(), IEventNotifications.UnsubscribeFromNumberChanged(), and ObserverSubject_NumberProducer.Update().
Referenced by Program.Run().