Facade

Give a complicated subsystem one simple front door, so most callers can use it without knowing how its parts fit together.

Description

A facade is a class with a few high-level methods, such as placeOrder(), that coordinates several subsystems behind the scenes: inventory, payments, shipping, email. It knows the right order, the data each part needs, and what to undo when a step fails.

Callers like a UI or an API controller make one call instead of orchestrating four services. The subsystems stay available for the rare caller that needs fine control; the facade simply makes the common path easy.

Class diagram

usesCheckoutPage+ onSubmit()CheckoutFacade+ placeOrder(lines, card, address, email)Inventory+ reserve(lines)+ release(lines)Payments+ authorize(card, amount)Shipping+ book(address)Mailer+ send(to, text)

Facade pattern class diagram

  1. Class CheckoutPage with + onSubmit().
  2. Class CheckoutFacade with + placeOrder(lines, card, address, email).
  3. Class Inventory with + reserve(lines), + release(lines).
  4. Class Payments with + authorize(card, amount).
  5. Class Shipping with + book(address).
  6. Class Mailer with + send(to, text).
  7. CheckoutPage uses CheckoutFacade.
  8. CheckoutFacade uses Inventory.
  9. CheckoutFacade uses Payments.
  10. CheckoutFacade uses Shipping.
  11. CheckoutFacade uses Mailer.

Sequence diagram

CheckoutPageCheckoutFacadeInventoryPaymentsShippingMailer1. placeOrder(…)2. reserve(lines)3. true4. authorize(card, 19.00)5. auth-19.006. book(address)7. tracking8. send(email, …)9. "order placed"

Facade pattern sequence diagram

  1. CheckoutPage → CheckoutFacade: placeOrder(…).
  2. CheckoutFacade → Inventory: reserve(lines).
  3. CheckoutFacade receives "true" back from Inventory.
  4. CheckoutFacade → Payments: authorize(card, 19.00).
  5. CheckoutFacade receives "auth-19.00" back from Payments.
  6. CheckoutFacade → Shipping: book(address).
  7. CheckoutFacade receives "tracking" back from Shipping.
  8. CheckoutFacade → Mailer: send(email, …).
  9. CheckoutPage receives ""order placed"" back from CheckoutFacade.

TypeScript example

A checkout facade over inventory, payments, shipping and email. It runs the steps in order and releases reserved stock if payment fails; the page makes one call.

facade.ts
// Facade: one simple placeOrder() call over four subsystems.

interface CartLine {
  sku: string;
  qty: number;
  price: number;
}

// Subsystems: each has its own detailed API
class Inventory {
  private readonly stock = new Map([["mug", 5], ["pen", 0]]);
  reserve(lines: CartLine[]): boolean {
    return lines.every((l) => (this.stock.get(l.sku) ?? 0) >= l.qty);
  }
  release(lines: CartLine[]): void {
    void lines; // put stock back (omitted)
  }
}

class Payments {
  authorize(card: string, amount: number): string | null {
    return card.startsWith("tok_") ? `auth-${amount.toFixed(2)}` : null;
  }
}

class Shipping {
  book(address: string): string {
    return `parcel to ${address}, tracking ZJ123`;
  }
}

class Mailer {
  send(to: string, text: string): void {
    void to;
    void text; // email provider call (omitted)
  }
}

// Facade: knows the right order and the failure handling
class CheckoutFacade {
  private readonly inventory = new Inventory();
  private readonly payments = new Payments();
  private readonly shipping = new Shipping();
  private readonly mailer = new Mailer();

  placeOrder(lines: CartLine[], card: string, address: string, email: string): string {
    if (!this.inventory.reserve(lines)) return "sorry, something is out of stock";
    const total = lines.reduce((sum, l) => sum + l.qty * l.price, 0);
    const auth = this.payments.authorize(card, total);
    if (!auth) {
      this.inventory.release(lines); // undo the reservation
      return "payment declined";
    }
    const parcel = this.shipping.book(address);
    this.mailer.send(email, `Thanks! ${parcel}`);
    return `order placed (${auth}), ${parcel}`;
  }
}

export function demo(): string[] {
  const checkout = new CheckoutFacade(); // the UI only talks to this
  return [
    checkout.placeOrder([{ sku: "mug", qty: 2, price: 9.5 }], "tok_4242", "Storgatan 1, Malmö", "sam@example.com"),
    checkout.placeOrder([{ sku: "pen", qty: 1, price: 2 }], "tok_4242", "Storgatan 1, Malmö", "sam@example.com"),
  ];
}

When to use it

Use it when

  • Many callers repeat the same multi-step sequence against a subsystem.
  • You want to put a stable, simple API in front of something complex or likely to change.
  • You're layering a system and want each layer to talk to the next through a narrow entry point.
  • You're wrapping a large library and only use a small part of it.

Avoid it when

  • The subsystem is already simple. A facade becomes a pass-through.
  • Callers genuinely need the fine-grained control. Forcing them through a facade means adding a method for every case.
  • The facade would coordinate across services over the network with transactions. That's a Saga or workflow, with its own failure handling.

Pitfalls

  • The god facade: one class that keeps growing until it holds all the business logic. Split by use case.
  • Hiding failures. A facade must still report clearly what went wrong, not swallow subsystem errors.
  • Callers bypassing the facade for some calls and using it for others, so the sequencing rules are enforced only sometimes.

Alternatives

Adapter

Choose it when you need one existing interface translated to another, not a simpler front for many parts.

Mediator

Choose it when the parts must talk to each other in both directions, and you want to centralise that conversation.

API Gateway

Choose it when the subsystems are separate services and clients need one network entry point.

Saga

Choose it when the steps span services and each needs a compensating action when a later step fails.