StructuralTypeScriptverifiedVerified

Facade Pattern in TypeScript

Provides a simplified, unified interface to a complex subsystem, hiding its internal complexity from clients.

How to Implement the Facade Pattern in TypeScript

1Step 1: Define the complex subsystem classes

class SubsystemA {
  operationA(): string { return "SubsystemA: ready"; }
  operationA2(): string { return "SubsystemA: go!"; }
}

class SubsystemB {
  operationB(): string { return "SubsystemB: ready"; }
  operationB2(): string { return "SubsystemB: fire!"; }
}

2Step 2: Create the Facade that orchestrates the subsystems

class Facade {
  private a: SubsystemA;
  private b: SubsystemB;

  constructor(a?: SubsystemA, b?: SubsystemB) {
    this.a = a ?? new SubsystemA();
    this.b = b ?? new SubsystemB();
  }

  simpleOperation(): string {
    const results = [
      "Facade initialises subsystems:",
      this.a.operationA(),
      this.b.operationB(),
      "Facade triggers subsystems:",
      this.a.operationA2(),
      this.b.operationB2(),
    ];
    return results.join("\n");
  }
}

3Step 3: Use the simplified Facade interface

function clientCode(facade: Facade): void {
  console.log(facade.simpleOperation());
}

clientCode(new Facade());

Facade Pattern Architecture

hourglass_empty

Rendering diagram...

lightbulb

Facade Pattern in the Real World

A hotel concierge is a facade for the city’s complex infrastructure. Instead of you directly calling a taxi company, booking a restaurant, and arranging a museum ticket separately, the concierge handles all of it through a single conversation. The underlying services still exist in their full complexity—you just don’t deal with them directly.