Member-only story

The Observer Pattern

Joseph Howerton
3 min readNov 27, 2020

--

High-Level Overview

At a high level, four constituents come together materializing as the observer pattern. An object called the observable manages a collection of dependents called observers and notifies them of any state changes. In most cases, communicating and manipulating information to/by the end-user is required. The observer pattern provides a counter-intuitive, but a reliable, model for capturing those state changes and presenting them to the end-user.

The Observable

If you have yet to realize the power of interfaces, here is another example. An interface and concrete implementation build an observable. Subscribing, unsubscribing, and notifying observers all fall under the scope of an observable. Data is pushed into an observable then redirected to all currently subscribed observers. The IObservable interface enforces this behavior.

public interface IObservable{    void subscribe(IObserver observer);
void unsubscribe(IObserver observer);
void notifyUpdate(Notification notification);
}

Managing dependencies is one of the two core behaviors of observables. Subscribe and unsubscribe enforce by the observable The subscribe and unsubscribe methods are used to add and remove observers to and from a list in the concrete subject class. Each…

--

--

No responses yet