Mastering the Observer pattern in JavaScript

Reading Time: 6 minutes

Mastering the Observer pattern in JavaScript

Introduction

The Observer pattern is a design pattern that allows an object (the observer) to be notified of changes to another object (the subject). This pattern is widely used in software development, and it can be implemented in various programming languages, including JavaScript. In this article, we will be discussing how to implement the Observer pattern in JavaScript, covering the browser (DOM), React, Node.js and plain JavaScript.

We will explore the similarities and differences between each implementation, and when it's best to use each one. We will start by discussing how events and callbacks are used to implement the pattern in the browser (DOM), and then we will move on to React and Node.js, to see how these environments handle events and callbacks differently.

Finally, we will discuss how to implement the pattern in plain JavaScript, without the use of any external libraries or frameworks. By the end of this article, you will have a good understanding of the Observer pattern in JavaScript and how to implement it in different environments.

Observer pattern in the browser (DOM)

The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the structure of a document as a tree of nodes, and provides an API for manipulating and interacting with the document. In the browser, events and callbacks are often used to implement the Observer pattern.

An event can be added to an element using the addEventListener method, which takes two arguments: the type of the event (such as 'click' or 'submit') and a callback function that will be called when the event is fired. For example, you can add an event listener to a button element that listens for a 'click' event and updates the text of a paragraph element when the button is clicked:

const button = document.getElementById("myButton");
const paragraph = document.getElementById("myParagraph");
let count = 0;

button.addEventListener("click", function() {
  count++;
  paragraph.innerHTML = "Button clicked " + count + " times";
});

In this example, the button element is the subject, and the callback function is the observer. When the button is clicked, the event is fired, and the callback function is called, updating the text of the paragraph element.

This is a basic example of how the Observer pattern can be implemented in the DOM. However, it's worth mentioning that the DOM API is not designed for complex state management. But it can be implemented in complex sequences if you fully understand the quirks of the inner workings.
.

Observer pattern in React

React is a JavaScript library that simplifies the process of building and maintaining complex user interfaces. In React, the Observer pattern is often implemented using component-based architecture and a unidirectional data flow. React components are similar to JavaScript objects, they have their own state and can subscribe to changes in other components' state. When a component's state changes, React automatically updates the corresponding elements in the DOM.

For example, you can create a button component that listens for a 'click' event and updates its own state when clicked:

import React, { useState } from 'react';

function MyButton() {
  // Declare a new state variable, which we'll call "count"
  // and set its initial value to 0
  const [count, setCount] = useState(0);

  return (
    <button onClick={() => setCount(count + 1)}>
    Click me! You have clicked me {count} times.
    </button>
  );
}

export default MyButton;

In this example, the button component is the subject, and the setCount function is the observer. When the button is clicked, the onClick event handler is called, and the setCount function is called, updating the count state of the component and re-render the component.

The main difference between the Observer pattern in the DOM and React is that in React, the observer is not a callback function, but a state-manipulating method, such as setState. Additionally, React handles the update of the DOM automatically, without the need of manually manipulating the DOM.

React provides a way to manage the state of your application and handle events in a more efficient and organized way. It makes it easier to manage and update the state of your components and it also allows you to handle events in a more declarative way, by providing a way to define event handlers as props on components. This allows for better separation of concerns and makes it easier to reason about the behavior of your application.

Observer pattern in Node.js

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It provides an event-driven, non-blocking I/O model that makes it well-suited for building event-driven applications. One of the most popular ways to implement the Observer pattern in Node.js is using the EventEmitter class. The EventEmitter class is a built-in module in Node.js that allows objects to emit and listen for events.

Here's an example of a simple observer and listener implementation in Node.js using the EventEmitter class:

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();

myEmitter.on('event', (data) => {
  console.log(`Received data: ${data}`);
});

myEmitter.emit('event', 'Hello World!');

In this example, we are extending the EventEmitter class to create a custom event emitter. Then we created an instance of the custom event emitter and added an event listener to it using the on method. The on method takes two arguments: the name of the event, and a callback function that will be called when the event is emitted.

Finally, we are emitting an event using the emit method and passing data to it. Once the event is emitted, all the listeners that are registered to that event will be called and passed the data as an argument.

It's worth mentioning that EventEmitter is not the only way to implement the observer pattern in Node.js, you can use other libraries such as rxjs to handle events and callbacks in a similar way.

Node.js provides an event-driven architecture that is well suited for building event-driven applications and the observer pattern is a good fit for this kind of architecture. It allows you to handle events and callbacks in a clean and organized way, and it also provides a way to emit events and pass data between different parts of your application.

Observer pattern in plain JavaScript

The Observer pattern can also be implemented in plain JavaScript without the use of any external libraries or frameworks. In this implementation, the subject and observer are both plain JavaScript objects. The subject holds a list of observer objects, and the observers hold a reference to the subject.

Here's an example of a simple observer and listener implementation in plain JavaScript:

class Subject {
  constructor() {
    this.observers = [];
  }

  subscribe(observer) {
    this.observers.push(observer);
  }

  unsubscribe(observer) {
    this.observers = this.observers.filter((obs) => obs !== observer);
  }

  notify(data) {
    this.observers.forEach((observer) => observer.update(data));
  }
}

class Observer {
  constructor(subject) {
    subject.subscribe(this);
    this.subject = subject;
  }

  update(data) {
    console.log(`Received data: ${data}`);
  }
}

const subject = new Subject();
const observer1 = new Observer(subject);
const observer2 = new Observer(subject);

subject.notify('Hello World!');

In this example, we created two classes, Subject and Observer. The Subject class holds a list of observer objects and has methods for subscribing, unsubscribing and notifying observers. The Observer class holds a reference to the subject and has an update method that will be called when the subject notifies the observer. The instances of these classes can be created and the subject can notify the observer with the data, and the observer can update the data.

Implementing the Observer pattern in plain JavaScript can be useful in situations where you don't want to use any external libraries or frameworks. However, it's worth noting that this implementation can be more verbose and less efficient than using a library or framework that is specifically designed for this pattern.

In summary, Implementing the Observer pattern in plain javascript can be useful in situations where you don't want to use any external libraries or frameworks, but it requires more code and it can be less efficient than using a library or framework that is specifically designed for this pattern.

Conclusion

In this article, we have explored how to implement the Observer pattern in JavaScript, covering the browser (DOM), React, Node.js and plain JavaScript. We have seen how events and callbacks are used to implement the pattern in the DOM, and how React uses a component-based architecture and unidirectional data flow to handle events and callbacks.

Additionally, we have seen how Node.js uses the EventEmitter class to handle events and callbacks, and how to implement the pattern in plain JavaScript without the use of any external libraries or frameworks.

We have also discussed the similarities and differences between each implementation, and when it's best to use each one. The DOM API is not designed for complex state management, React is a good choice for handling complex state management, Node.js is well suited for building event-driven applications and plain javascript implementation is useful in situations where you don't want to use any external libraries or frameworks.

The Observer pattern is a powerful design pattern that can be used in various environments and can be implemented in different ways, each with its own advantages and limitations. Understanding the Observer pattern and how it can be implemented in JavaScript can help you design more efficient, organized and maintainable code.

0 Shares:
You May Also Like
Read More

Exposure: Why it matters?

Reading Time: 4 minutes Your personal brand should demonstrate your authentic talents and strengths. How you expose your self to the world…
Read More

Fear the nothing!

Reading Time: 4 minutes Taking risks is a common thing on a daily basis. We're taking risks when we walk on a…