For AI agents: the complete documentation index is available at https://nhanbe.id.vn/llms.txt, the full documentation bundle is available at https://nhanbe.id.vn/llms-full.txt, and this page is available as Markdown at https://nhanbe.id.vn/design-patterns/structural/facade.md.

Provide a simplified interface to a complex subsystem. You want a clean API for a complex set of classes. You want to decouple clients from

Facade

Overview

Provide a simplified interface to a complex subsystem.

When to use

  • You want a clean API for a complex set of classes.
  • You want to decouple clients from subsystem details.

Java example

class PaymentFacade {
    private final FraudCheck fraud = new FraudCheck();
    private final PaymentGateway gateway = new PaymentGateway();

    public boolean pay(String userId, int amount) {
        if (!fraud.isAllowed(userId, amount)) {
            return false;
        }
        return gateway.charge(userId, amount);
    }
}

TypeScript example

class PaymentFacade {
  private fraud = new FraudCheck();
  private gateway = new PaymentGateway();

  pay(userId: string, amount: number): boolean {
    if (!this.fraud.isAllowed(userId, amount)) {
      return false;
    }
    return this.gateway.charge(userId, amount);
  }
}

Pros and cons

Pros:

  • Simplifies client usage.
  • Reduces coupling.

Cons:

  • Can become a god object.
  • May hide important capabilities.

Common pitfalls

  • Putting business logic into the facade.
  • Growing the facade instead of creating focused APIs.