The Goal
Hide a complex, ugly system behind a simple, clean method call.
Imagine you are placing an online order. Behind the scenes, the system has to check inventory, charge a credit card, and generate a shipping label.
- You don’t want the client application to have to orchestrate all of that. You build a Facade to wrap it structurally.
- THE COMPLEX SUBSYSTEM
public class InventoryService {
public boolean checkStock(String item) {
return true;
}
}
public class PaymentGateway {
public void chargeCard(double amount) {
System.out.println("Card charged.");
}
}
public class ShippingService {
public void dispatch(String item) {
System.out.println("Item shipped.");
}
}
- THE STRUCTURAL WRAPPER (Facade)
public class OrderProcessingFacade {
private final InventoryService inventory = new InventoryService();
private final PaymentGateway payment = new PaymentGateway();
private final ShippingService shipping = new ShippingService();
// The client only calls this ONE simple method
public void placeOrder(String item, double price) {
if (inventory.checkStock(item)) {
payment.chargeCard(price);
shipping.dispatch(item);
System.out.println("Order complete!");
}
}
}
Why this is Structural
-
Look at the subsystem classes (
InventoryService,PaymentGateway, etc.). -
They don’t have an
OrderProcessingFacadepassed into their constructors. -
They don’t know the Facade exists.
-
The Facade is just a structural container built on top of them to make the client’s life easier.
-
Communication flows in one direction: Top-Down.