Facade Design Pattern
Facade design pattern provides a simplified, unified interface to a complex subsystem, acting as a single entry point to hide internal intricacies and reduce client-side dependencies, making the system easier to use and maintain, much like a remote control simplifies controlling a home theater system. It’s a structural pattern that wraps a complex set of classes or APIs with a single, cleaner class, offering only the most relevant functionality to clients.
How it works
- A Facade Class: A single class that provides simplified methods (e.g.,
playMovie(),setupHomeTheater()). - Subsystem Classes: The complex, underlying components (e.g.,
Television,SoundSystem,StreamingDevice). - Delegation:
- The Facade methods call multiple methods in the subsystem classes to perform a combined action, shielding the client from this complexity
Example: Home Automation System
- Without Facade: A client would need to interact with
Light.turnOn(),TV.setSource(HDMI1),SoundSystem.setVolume(30), etc.. - With Facade: A
HomeAutomationFacadeoffers awatchMovie()method that internally calls all the necessary subsystem methods in the correct order, requiring only one call from the client.
Get Kaizen Chandra’s stories in your inbox
Join Medium for free to get updates from this writer.
Key benefits
- Reduces Coupling: Clients depend on the single Facade, not many subsystem classes, making changes easier.
- Simplifies Usage: Provides an easy-to-understand interface for complex features.
- Improves Maintainability: Isolates complexity within the Facade.
- Handles Dependencies: Moves unwanted dependencies to one place.
Common use cases
- Wrapping complex libraries or frameworks (e.g., a video conversion library).
- Creating an API gateway for microservices.
- Simplifying database operations or network functions.
This video demonstrates how to use the facade pattern to reduce coupling:
// Inventory Service
class InventoryService {
public boolean checkStock(String productId) {
System.out.println("Checking stock for product: " + productId);
return true; // assume available
}
}
// Payment Service
class PaymentService {
public boolean processPayment(String account, double amount) {
System.out.println("Processing payment of Rs " + amount);
return true; // assume success
}
}
// Invoice Service
class InvoiceService {
public void generateInvoice(String productId) {
System.out.println("Generating invoice for product: " + productId);
}
}
// Shipping Service
class ShippingService {
public void shipProduct(String productId) {
System.out.println("Shipping product: " + productId);
}
}// Facade Class
class OrderFacade {
private InventoryService inventoryService;
private PaymentService paymentService;
private InvoiceService invoiceService;
private ShippingService shippingService;
public OrderFacade() {
inventoryService = new InventoryService();
paymentService = new PaymentService();
invoiceService = new InvoiceService();
shippingService = new ShippingService();
}
public void placeOrder(String productId, String account, double amount) {
if (inventoryService.checkStock(productId)) {
if (paymentService.processPayment(account, amount)) {
invoiceService.generateInvoice(productId);
shippingService.shipProduct(productId);
System.out.println("Order placed successfully!");
} else {
System.out.println("Payment failed!");
}
} else {
System.out.println("Product out of stock!");
}
}
}








