Design Pattern Examples
Overview of object-oriented design patterns
ObserverSubject_NumberProducer.cs File Reference

The IObserverNumberChanged, IEventNotifications, and INumberProducer interfaces, and the ObserverSubject_NumberProducer class used in the Observer pattern. More...

Go to the source code of this file.

Classes

interface  IObserverNumberChanged
 Represents an observer to the Subject class. An observer implements this interface and then subscribes to the Subject with the interface. The observer will be called whenever a change in the number is made. More...
 
interface  IEventNotifications
 Represents a Subject that takes observers implementing the IObserverNumberChanged interface. More...
 
interface  INumberProducer
 Represents the Subject to the observers. This is the minimum needed by observers to get access to the data provided by the Subject class. More...
 
class  ObserverSubject_NumberProducer
 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...
 

Namespaces

namespace  DesignPatternExamples_csharp
 The namespace containing all Design Pattern Examples implemented in C#.
 

Detailed Description

The IObserverNumberChanged, IEventNotifications, and INumberProducer interfaces, and the ObserverSubject_NumberProducer class used in the Observer pattern.

The Observer pattern is used when one or more entities need to be told about a change in state of another entity (typically known as the Subject) and those entities, upon notification, pull data from the Subject to complete their own specific tasks. Alternatively, the Subject could push the data to the observing entities. The example uses a pull approach.

The code in this file defines the Subject for the example. In addition, this file contains the interfaces needed for the observers and the Subject to interact "at arms length" from each other, so neither has any more information about the other than is strictly necessary.

Definition in file ObserverSubject_NumberProducer.cs.