The Observer Pattern In Javascript

  • The observer pattern, also referred to as the publish/subscribe pattern, is a design pattern where an object (called the subject or observable), will maintain a list of “dependents” called observers.
  • This video is a good summary of the Observer pattern chapter in the book Head First Design Patterns book.
  • VIDEO

    Redux: A use case for the observer pattern
    If you work in the world of React, you have probably dealt with Redux to manage the state of your application.

  • Redux is an implementation of the observer pattern.
  • This method “pushes” new subscribers to the array

    const subscribe = (listener) = (
        listeners.push(listener)
    );

    Remember that subscribe is called in the componentDidMount method which fires immediately after the component mounts.

Continue reading “The Observer Pattern In Javascript”