Open In App

Facade Design Pattern | JavaScript Design Pattern

Facade design pattern is a Structural design pattern that allows users to create a simple interface that hides the complex implementation details of the system making it easier to use. This pattern intends to create a simplified and unified interface for a set of interfaces hiding the implementation details making it less complex to use.



Components of the Facade Design Pattern

The components of the Facade design pattern include:



Implementation of the Facade design pattern using JavaScript

Let us understand the process of Implementing a Facade design pattern with an example:

Step 1: Creation of Subsystem




class SubsystemA {
  method() {
    console.log('This is a method of Subsystem-A');
  }
}
 
class SubsystemB {
  method() {
    console.log('This is a method of Subsystem-B');
  }
}
 
class SubsystemC {
  method() {
    console.log('This is a method of Subsystem-C');
  }
}

Explanation:

In this step, I have created the Subsystem which contains three different classes namely `SubsystemA`, `SubsystemB` and` SubsystemC` along with the methods which console logs the information about which method they belong to.

Step 2: Creation of the Facade




class Facade {
  constructor() {
    this.subsystemA = new SubsystemA();
    this.subsystemB = new SubsystemB();
    this.subsystemC = new SubsystemC();
  }
 
  commonInterface() {
    this.subsystemA.method();
    this.subsystemB.method();
    this.subsystemC.method();
  }
}

Explanation:

In this step, I have created a `Facade` class that contains a constructor that intialies the objects of the three Substystems i.e. `SubsystemA`, `SubsystemB`, `SubsystemC` respectively. The `Facade` class is also responsible for providing a common interface. So, I created a method called `commonInterface` which hides the implementation details of subsystem and serves as a entry point.

Step 3: Usage and the Client code




const facade = new Facade();
facade.commonInterface();

Explanation:

In this step, I have created a `facade` object for the Facade class and calling the `commonInterface` method of the `Facade` class. Now, the actual process starts without need of knowing the implementation details.

Below is the complete code for the Facade Design Pattern in Javascript:




class SubsystemA {
  method() {
    console.log('This is a method of Subsystem-A');
  }
}
 
class SubsystemB {
  method() {
    console.log('This is a method of Subsystem-B');
  }
}
 
class SubsystemC {
  method() {
    console.log('This is a method of Subsystem-C');
  }
}
class Facade {
  constructor() {
    this.subsystemA = new SubsystemA();
    this.subsystemB = new SubsystemB();
    this.subsystemC = new SubsystemC();
  }
 
  commonInterface() {
    this.subsystemA.method();
    this.subsystemB.method();
    this.subsystemC.method();
  }
}
const facade = new Facade();
facade.commonInterface();

Output:

output

Below is the Diagrammatic Representation of the Facade Design Pattern:

Let’s visualize the above example using UML components.

Advantages of the Facade Design Pattern in JavaScript Design Patterns

Disadvantages of the Facade Design Pattern in JavaScript Design Patterns


Article Tags :