Command Palette

Search for a command to run...

PHASE 2Beginner ~7 min· topic 8 of 22Structural

Structural 3 — Facade

In one line

One simplified entry point that hides a messy subsystem of many classes.

0/22 · 0%

Think of it like this

A hotel reception desk. You say 'I need a taxi at 6 AM' and the desk talks to the taxi company, the wake-up service and the kitchen for you. One simple front door to many complex systems.

Key ideas

  1. 01

    Solves: client coupling to a dozen classes (config, auth, retry, serialize) just to 'send a message'.

  2. 02

    Facade is a simplified interface to a complex subsystem — not a wrapper that adds behavior.

  3. 03

    Differs from Adapter: facade simplifies an existing system; adapter translates to a foreign interface.

Java / Spring map

  • →

    Spring's JdbcTemplate is a facade over JDBC/DataSource plumbing; SLF4J is a facade over logging back-ends.

Code & diagrams

CheckoutFacade.javajava

One call for the controller; the orchestration of four subsystems stays in one place.

public final class CheckoutFacade {
  private final InventoryClient inventory;
  private final PaymentService payments;
  private final OrderRepository orders;
  private final NotificationService notifications;

  public CheckoutFacade(InventoryClient i, PaymentService p,
                        OrderRepository o, NotificationService n) {
    this.inventory = i; this.payments = p; this.orders = o; this.notifications = n;
  }

  public OrderId checkout(Cart cart, PaymentMethod method) {
    Reservation r = inventory.reserve(cart.items());          // 1
    try {
      ChargeResult c = payments.charge(cart.total(), method);  // 2
      Order order = orders.save(Order.from(cart, c));          // 3
      notifications.orderConfirmed(order);                     // 4
      return order.id();
    } catch (RuntimeException e) {
      inventory.release(r);                                    // compensate
      throw e;
    }
  }
}
// Controller: checkoutFacade.checkout(cart, method); — knows nothing of the four subsystems.

Explain without notes

01

Facade vs Adapter vs Decorator — one-line distinction for each.

Practice

01

Design a CheckoutFacade hiding OrderService + PaymentService + InventoryClient + EmailService.

Trade-offs

  • ↔

    A greedy facade can become the god class the SRP warns about. Keep it as thin orchestration with no business rules.

Completion checklist

  • I can explain Facade vs Adapter in one sentence each.

Back to phase