pub fn observer_exercise() -> Result<(), String>
Expand description

Example of using the “Observer” design pattern.

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.