Open In App

Behavioral Design Pattern | JavaScript Design Pattern

Behavioral design patterns are a subset of design patterns in software engineering that deal with the interaction and responsibilities of objects. These patterns focus on how objects communicate and work together to achieve common tasks.

Uses Cases of the Behavioral Design Pattern

Example of the Behavioral Design Pattern in JavaScript Design Pattern:

Problem Statement:

We can take the example of news design here news notification system that allows news agencies to notify multiple news readers about the latest news updates. The system should provide flexibility and maintainability for future extensions and should utilize the Observer behavioral design pattern to handle the interactions between news agencies and news readers.



Below is the implementation of the above example:




// Subject
class NewsAgency {
  constructor() {
    this.observers = [];
    this.news = null;
  }
 
  registerObserver(observer) {
    this.observers.push(observer);
  }
 
  removeObserver(observer) {
    this.observers = this.observers.filter(o => o !== observer);
  }
 
  setNews(news) {
    this.news = news;
    this.notifyObservers();
  }
 
  notifyObservers() {
    this.observers.forEach(observer => observer.update(this.news));
  }
}
 
// Observer
class NewsReader {
  constructor(name) {
    this.name = name;
  }
 
  update(news) {
    console.log(`${this.name} received news: ${news}`);
  }
}
 
// Usage
const agency = new NewsAgency();
const reader1 = new NewsReader("Reader 1");
const reader2 = new NewsReader("Reader 2");
 
agency.registerObserver(reader1);
agency.registerObserver(reader2);
 
agency.setNews("Breaking news: Behavioral Design Patterns in JavaScript!");

Output
Reader 1 received news: Breaking news: Behavioral Design Patterns in JavaScript!
Reader 2 received news: Breaking news: Behavioral Design Patterns in JavaScript!




Key Component of Behavioral Design Pattern

These are the following components of behavioral design patterns:

Step-by-step Code Explanation for the above Example

NewsAgency Class:




class NewsAgency {
  constructor() {
    this.observers = [];
    this.news = null;
  }

The NewsAgency class is the subject in the Observer pattern.It has a constructor that initializes an empty array observers to store registered observers and news to store the latest news update.

Register Observer Method:




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

The registerObserver method allows news agencies to register news readers as observers. It adds the provided observer to the list of observers.

Remove Observer Method:




removeObserver(observer) {
   this.observers = this.observers.filter(o => o !== observer);
 }

The removeObserver method enables news agencies to unregister observers. It removes the provided observer from the list of observers.

SetNews Method:




setNews(news) {
   this.news = news;
   this.notifyObservers();
 }

The setNews method allows news agencies to set the latest news content. It updates the news property and then calls the notifyObservers method to notify all registered observers.

Notify Observer Method:




notifyObservers() {
    this.observers.forEach(observer => observer.update(this.news));
  }

The notifyObservers method iterates through the list of observers and calls the update method of each observer, passing the latest news as a parameter.

NewsReader Class:




class NewsReader {
  constructor(name) {
    this.name = name;
  }

The NewsReader class represents news readers, which are the observers in the Observer pattern.It has a constructor that accepts a name parameter to set the name of the news reader.

Update Method:




update(news) {
    console.log(`${this.name} received news: ${news}`);
  }

The update method is called by the NewsAgency when news updates are available. It logs a message to the console, indicating that the news reader with the specified name has received the news.

Usage:




const agency = new NewsAgency();
const reader1 = new NewsReader("Reader 1");
const reader2 = new NewsReader("Reader 2");
 
agency.registerObserver(reader1);
agency.registerObserver(reader2);
 
agency.setNews("Breaking news: Behavioral Design Patterns in JavaScript!");

In the usage section, we create instances of the NewsAgency and two NewsReader objects.We register reader1 and reader2 as observers with the agency using the registerObserver method..

Diagrammatical Explanation of the above Example

Diagrammatical Representation of the given Example

Lets understand the example:

Advantages of the Behavioral Design Pattern

Disadvantages of the Behavioral Design Pattern

Conclusion

Behavioral design patterns are essential for structuring the interactions between objects in your software. They provide a foundation for creating flexible and maintainable code. When choosing a behavioral pattern, consider the specific requirements of your system and choose the pattern that best fits the problem at hand. Use these patterns judiciously, as excessive use can lead to unnecessary complexity.


Article Tags :