← Back to Home

Facade Pattern

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

What is it?

The Facade pattern simplifies interactions with complex systems by providing a unified and minimal API that hides the underlying complexity.

Example

class CPU {
  freeze() { return 'Freezing CPU'; }
  jump(position: number) { return `Jumping to ${position}`; }
  execute() { return 'Executing instructions'; }
}

class Memory {
  load(position: number, data: string) {
    return `Loading ${data} into position ${position}`;
  }
}

class HardDrive {
  read(position: number, size: number) {
    return `Reading ${size} bytes from position ${position}`;
  }
}

class ComputerFacade {
  private cpu = new CPU();
  private memory = new Memory();
  private hardDrive = new HardDrive();

  start() {
    return [
      this.cpu.freeze(),
      this.memory.load(0, 'bootloader'),
      this.hardDrive.read(0, 256),
      this.cpu.jump(0),
      this.cpu.execute()
    ];
  }
}

const computer = new ComputerFacade();
console.log(computer.start());

Common Uses

  • Hiding complex library calls behind a simple API
  • Startup/shutdown sequences
  • Unifying legacy APIs under a single entry point

When to Use

  • When you need to simplify complex subsystems
  • To shield client code from implementation details

Caution

  • Can hide important details from developers
  • Facade can become god object if it does too much