Facade Pattern

Posted by Dustin Boston in .

The Facade Pattern is a structural design pattern that simplifies interactions with complex systems by providing a unified and easy-to-use interface.


The Facade Pattern is a structural design pattern that provides a simplified interface to a larger body of code, making complex systems easier to use. By creating a unified and easy-to-use interface, this pattern reduces the complexity of interactions with subsystems, improving code readability and maintainability. It is particularly useful when working with systems that have multiple interdependent components.

Source Code Listing

code.ts

class Subsystem1 {
  constructor() {
    console.log("subsystem1");
  }

  operation1(): void {
    // Added a method to Subsystem1 to show usage
    console.log("Subsystem1 operation");
  }
}

class Subsystem2 {
  constructor() {
    console.log("subsystem2");
  }

  operation2(): void {
    // Added a method to Subsystem2 to show usage
    console.log("Subsystem2 operation");
  }
}

class Facade {
  request(): void {
    const s1 = new Subsystem1();
    const s2 = new Subsystem2();

    s1.operation1(); // Example of using the subsystems
    s2.operation2();
  }

  // Facade can also provide simplified interfaces for common use cases
  complexOperation(): void {
    const s1 = new Subsystem1();
    const s2 = new Subsystem2();
    s1.operation1();
    s2.operation2();
    console.log("Complex operation finished");
  }
}

const client = {
  run(): void {
    const facade = new Facade();
    facade.request();
    facade.complexOperation(); // Demonstrating the simplified interface
  },
};

client.run();