Facade

Posted by Dustin Boston .


CAUTION

Construction Area
Watch out for:

  • Broken code
  • No comments
  • Partial examples
  • Missing tests

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();