Explain the Facade pattern.
Topic: design-patterns, Structural Patterns. Level: Basic.
Answer
Facade provides a **simple, unified interface** to a complex subsystem, hiding its internal classes and interactions behind one entry point. Clients use the easy facade instead of orchestrating many low-level objects, reducing coupling and cognitive load. It doesn't add functionality — it simplifies access. Example: an `OrderFacade.placeOrder()` that internally coordinates inventory, payment, and shipping services.
```java
// Complex subsystem
class Inventory { void reserve(String id){ /*...*/ } }
class Payment { void charge(String id){ /*...*/ } }
class Shipping { void schedule(String id){ /*...*/ } }
// Facade: one simple method hides the orchestration
class OrderFacade {
private final Inventory inv = new Inventory();
private final Payment pay = new Payment();
private final Shipping ship = new Shipping();
public void placeOrder(String id) { // client calls just this
inv.reserve(id); pay.charge(id); ship.schedule(id);
}
}
new OrderFacade().placeOrder("A1"); // client unaware of subsystem details
```