DEV Community

Nishan Bajracharya
Nishan Bajracharya

Posted on • Originally published at Medium on

Implementing the Observer pattern in JavaScript

Shamelessly borrowed from Wikipedia

The observer pattern is a design pattern that observes an entity and on change, notifies all subscribers of the change. It is very simple to understand and is very effective for when a change in an entity needs to trigger many events.

How does the observer pattern work?

The observer pattern is a combination of two elements, a Subject, and an Observer. The Subject tracks the value of an entity and the Observer listens for changes tracked by the Subject.

When an entity tracked by the Subject changes, the Subject notifies the Observer of the change. One Subject can have many Observers, in which case it notifies all the Observers of the change in the entity. The observers can be subscribed to trigger events when the Subject notifies them.

So how do you implement the observer pattern in JavaScript?

Let’s start with the Subject, which is alternatively called an Observable. The subject contains a state and a collection of observers that it notifies to when the state changes.

The subject provides methods to register and unregister observers and a method to notify all the observers.

Each observer has a notify method which the subject calls when it needs to notify observers for change in state.

Next, let’s look at the Observer. It simply only contains a notify method but we add a subscribe method that is used to subscribe an event to the Observer. When the Subject notifies the Observer of change in state, the Observer triggers the subscribers.

The subscriber is a function that gets called when the Observer’s notify function is called.

How do you use it?

We create a new subject that tracks the value of an entity and an observer for the subject an subscribe to the observer. Then when there is a change in the subject’s state, all the observers are notified and their subscribers are triggered.

The subject has two observers and each observer is subscribed by two functions. When the observable changes the state by calling the setState method, both the observers are notified and each of their subscribers are triggered.

And that is how the observer pattern works and how it is implemented in JavaScript.

An alternate and simpler approach

While the above approach to implementing the observer pattern is more than enough, it requires creating at least one instance of the subject and one instance of the observer to work. So we can simplify the pattern using a single Observer class that maintains the state and also triggers the subscribers of the observer.

What are some practical use cases of the Observer Pattern?

When done correctly, the observer pattern can be very powerful and extremely helpful for broadcasting information across an application. Some use cases of the observer pattern can be like so:

  • Group Messaging: When a message is sent to a group chat, all the participants of the chat receive the notification of the new message.
  • Website Newsletters: All subscribers of a website newsletter receive email notifications when the website publishes a new newsletter.
  • Redux: When a redux action is dispatched, all the subscribers of the store are notified.

References and Further Learning

Discussion (0)