Structural Design Pattern
Facade Design Pattern in Java
Provide a simple interface to a complex subsystem.
In this lesson: Facade hides subsystem coordination behind a small API. Clients call the facade instead of knowing the order and details of several internal services.
Overview
Facade is useful when its recurring design problem appears in a real application. It keeps the relevant responsibilities organized while allowing surrounding code to depend on clear abstractions.
Implementation
final class Inventory { boolean available(String sku) { return true; } }
final class Payment { void charge(String card) { } }
final class Shipping { void ship(String address) { } }
final class OrderFacade { private final Inventory inventory = new Inventory(); private final Payment payment = new Payment(); private final Shipping shipping = new Shipping(); void place(String sku, String card, String address) { if (inventory.available(sku)) { payment.charge(card); shipping.ship(address); } } }
new OrderFacade().place("JAVA-1", "card", "Hyderabad");
When should you use it?
Use Facade to simplify legacy APIs, service orchestration, library setup, or any subsystem with too many details for its callers.
Next step: Continue with the Design Patterns course and explore the next pattern.