Facade
Providing a simple, unified interface over a complex subsystem — examples, layering, facades vs service classes, and the risk of god objects.
Intent: provide a unified, simplified interface to a set of interfaces in a subsystem. The facade makes the subsystem easier to use for common cases, without hiding it from clients that need full control.
Analogy: a hotel concierge. You say "book me dinner and a taxi at 7"; the concierge deals with the restaurant, the taxi company and the timing.
Example
Without a facade, every caller (web controller, mobile API, admin tool) repeats the same orchestration — and gets it subtly wrong.
public class OrderFacade {
private final InventoryService inventory;
private final PaymentService payments;
private final ShippingService shipping;
private final NotificationService notifications;
public OrderFacade(InventoryService i, PaymentService p, ShippingService s, NotificationService n) {
this.inventory = i; this.payments = p; this.shipping = s; this.notifications = n;
}
public OrderConfirmation placeOrder(Cart cart, User user) {
Reservation r = inventory.reserve(cart.items());
try {
Payment pay = payments.charge(user.paymentMethod(), cart.total());
Shipment ship = shipping.schedule(cart.items(), user.address());
notifications.orderPlaced(user, ship.trackingId());
return new OrderConfirmation(pay.id(), ship.trackingId());
} catch (RuntimeException e) {
inventory.release(r); // the facade owns the error handling too
throw e;
}
}
}class OrderFacade:
def __init__(self, inventory, payments, shipping, notifications):
self.inventory, self.payments = inventory, payments
self.shipping, self.notifications = shipping, notifications
def place_order(self, cart, user) -> OrderConfirmation:
reservation = self.inventory.reserve(cart.items)
try:
payment = self.payments.charge(user.payment_method, cart.total)
shipment = self.shipping.schedule(cart.items, user.address)
self.notifications.order_placed(user, shipment.tracking_id)
return OrderConfirmation(payment.id, shipment.tracking_id)
except Exception:
self.inventory.release(reservation)
raiseclass OrderFacade {
constructor(
private inventory: InventoryService,
private payments: PaymentService,
private shipping: ShippingService,
private notifications: NotificationService,
) {}
async placeOrder(cart: Cart, user: User): Promise<OrderConfirmation> {
const reservation = await this.inventory.reserve(cart.items);
try {
const payment = await this.payments.charge(user.paymentMethod, cart.total);
const shipment = await this.shipping.schedule(cart.items, user.address);
await this.notifications.orderPlaced(user, shipment.trackingId);
return { paymentId: payment.id, trackingId: shipment.trackingId };
} catch (e) {
await this.inventory.release(reservation);
throw e;
}
}
}Real-world facades
- Libraries:
requestsoverurllib3/sockets; jQuery over the DOM;fetchover HTTP plumbing. - Frameworks: Spring's
JdbcTemplateover raw JDBC (connections, statements, result sets, exception translation). - Compilers:
javacas one command over lexer, parser, type checker and code generator. - Application services in layered architecture: a thin layer exposing use cases over domain objects and repositories.
- API gateways / BFFs: one simple endpoint aggregating several back-end services.
Facade vs related patterns
| Pattern | Interface | Purpose |
|---|---|---|
| Facade | New, simpler | Make a subsystem easy to use |
| Adapter | Converts to an existing expected interface | Make incompatible things fit |
| Mediator | Coordinates peers that talk through it | Reduce coupling between components that know each other |
Key takeaways
- Facade gives a simple entry point to a complex subsystem.
- It orchestrates calls and error handling so clients don't repeat it.
- The subsystem stays accessible for advanced use.
- Keep facades thin; split them before they become god objects.