Observer Pattern
Explore the Observer pattern by building a simple Producer and Listeners. Understand how this design decouples event notifications and enhances asynchronous programming with RxJS. Gain insight into the core reactive concept that supports observable behavior.
We'll cover the following...
For a software developer, it’s hard to hear about Observables and not think of the venerable Observer pattern. In it, we have an object called Producer that keeps an internal list of Listeners subscribed to it. Listeners are notified, by calling their update method, whenever the state of the Producer changes. In most explanations of the Observer pattern, this entity is called Subject, but to avoid confusion with RxJS’s own Subject type, we call it Producer.
It’s easy to implement a rudimentary version of the pattern in a few lines:
Let’s see the code:
The Producer object keeps a dynamic list of Listeners in the instance’s listeners array, which will be updated whenever the Producer calls its notify method. In the following code, we create two objects that listen to notifier, an instance of Producer:
Run the following program:
Both listener1 and listener2 are notified whenever the Producer notifier updates its internal state, without us having to check for it.
Our implementation is simple, but it illustrates how the Observer pattern allows decoupling between the events and the listener objects that react to them.