...

/

Observer Design Pattern

Observer Design Pattern

Let's explore the methodology behind RxJs.

Making use of the Observer Design pattern

RxJS uses the Observer Design pattern, an official design pattern from the Gang of Four.

📝 Note: The Gang of Four is in reference to a group of four developers, Erich Gamma, Richard Helms, Ralph Johnson, and John Vlissides, who in 1994 worked together on a book called Design Patterns: Elements of Reusable object-orientated Programming. Here, they set out 23 design patterns or approaches for writing good object-orientated software, which, in their opinion, had some major problems at that time.

This book has become a classic computer programming book, and now over 25 years later, the approaches they described in this book are still being used, including the Observer Design pattern.

Problems solved by an Observer design pattern

The Observer Design pattern solves the following problems:

  • A one-to-many dependency between objects should be defined without making the objects tightly-coupled.
  • It should be ensured that when one object changes state, an open-ended number of dependent objects are updated automatically.
  • It should be possible that one object can notify an open-ended number of other objects.

The Observer pattern aims to solve the problem where we have one object that needs to inform many other objects of any changes, whether they are data changes or errors.

Reactive programming uses this Observer design pattern by allowing us as developers to create data streams and then set any number of Observer objects to listen to this data over time. If this stream of data changes, then the Observer objects react to this.