The Observer Pattern: Keeping your Objects in the know

The observer design pattern enables a subscriber to register with and receive notifications from a provider. It is suitable for any scenario that requires push-based notification. The pattern defines a provider (also known as a subject or an observable) and zero, one, or more observers. Observers register with the provider, and whenever a predefined condition, event, or state change occurs, the provider automatically notifies all observers by calling one of their methods. In this method call, the provider can also provide current state information to observers. The observer design pattern is applied by implementing the generic System.IObservable and System.IObserver interfaces. The generic type parameter represents the type that provides notification information.

Observer Design Pattern

Misguided approach at a Weather Station

Here’s a first implementation possibility—we’ll take the hint from the Weather-O-Rama developers and add our code to the measurementsChanged() method:

What is wrong with our implementation