Back to the book of the four horsemen of Javapocalypse, they defined the Observer pattern as a way to define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
It can seem strange, but it’s a pattern you probably already use if you play with JS listeners, because Listener is another name of this pattern. So this code :
uses the Observer pattern.
As explained by the Java boys, you can create a component that can trigger some code asynchronously in another component. This pattern is one of the best in JS and a fundamental of Reactive Programming.
Note: other names of this pattern are Publisher, Subscriber or Listener.
The basic version
This first iteration of the pattern does the same job as the book’s initial version: trigger something at a specific moment.
We have two components: the subject (the thing which triggers something) and the listener (the triggered thing). For example, if you design a theme management service for your web app, you want the style of your application to be updated when the theme changes. Somewhere you will have your application renderer, which listens to changes of the theme management service.
In JS, we will use functions to make the pattern working.
Subject
Listener
Explanation
Note: I use a Singleton for the service as described in a previous article.
Each time you call the ThemeService.onChange
function, you add a new listener to the ThemeService. At the moment you call ThemeService.changeTheme
, the ThemeService will call all the listeners.
In the present situation, we will re-render our application when there’s a theme change. It’s a simple way to make a reactive application without any framework.
The “multiple events” version
Now, imagine we have to implement a module to change icons independently from the styles. If we use the previous implementation, we have to add a separate array to store the icons listener and add a function like onChange
. That’s not very practical.
If you remember EventListener on a DOM object, you know you can add a specific event listener by specifying its name in the addEventListener(event, listener)
function. That’s the proper way to implement a multi-event system.
Subject
Listener
Explanation
It seems there’s a lot of changes. In reality, there are few significant changes:
this.listeners
is now an object.ThemeService.onChange
andThemeService._notifyListeners
take an event name to know which event triggers the listener passed in parameter.- when we notify listeners with
ThemeService._notifyListeners
, we pass an event as a second parameter. This event will be sent to every listener listening to this event. - our listeners use the data from the event object.
And now we have an easily extensible ThemeService
with the basics of reactive programming.
I hope you found this article helpful. See you soon!