The Facade Pattern is a structural design pattern that provides a unified, simplified interface to a set of interfaces in a subsystem. It hides the complexity of the subsystem and provides clients with a simple interface to interact with it.
- Simplification: Hide subsystem complexity behind simple interface
- Loose Coupling: Decouple clients from subsystem components
- Easy to Use: Provide convenient methods for common operations
- Maintenance: Changes to subsystem don't affect clients
- Layering: Create logical layers in complex systems
// WITHOUT Facade - Client must know all details
LightingSystem lights = new LightingSystem();
ACSystem ac = new ACSystem();
MusicSystem music = new MusicSystem();
SecuritySystem security = new SecuritySystem();
// Client must manage complex interactions
lights.turnOn();
ac.setTemperature(22);
music.play("Wake up song");
security.disarmAlarm();
// What if we add more systems? Code becomes messy!
// WITH Facade - Simple unified interface
SmartHome home = new SmartHome();
home.goodMorning(); // One call handles everything!New developers must understand entire subsystem to use it.
Subsystem interaction logic scattered throughout application.
- Facade: Only provides simplified interface
- Subsystems: Handle their specific responsibilities
- Each component has clear purpose
- Client depends on Facade interface
- Not on complex subsystem interfaces
- Facade open for extension (new methods)
- Subsystems closed for modification
// Complex and error-prone - client must know everything
void orderFood() {
// Kitchen operations
Chef chef = new Chef();
chef.prepareDish("Pizza");
// Waiter operations
Waiter waiter = new Waiter();
waiter.takeOrder();
// Cashier operations
Cashier cashier = new Cashier();
cashier.processPayment(amount);
// Delivery operations
DeliveryPerson delivery = new DeliveryPerson();
delivery.deliverFood();
// What if order of operations is wrong? Client must handle all details!
}// Simple and robust - facade handles complexity
void orderFood() {
Restaurant restaurant = new Restaurant();
restaurant.orderAndDeliver("Pizza", address); // One call!
}
// Facade internally handles all the complexity
public class Restaurant {
void orderAndDeliver(String dish, String address) {
chef.prepareDish(dish);
waiter.takeOrder();
cashier.processPayment();
delivery.deliverFood();
}
}✓ Simplified Interface: Provides easy-to-use interface ✓ Hides Complexity: Internal subsystem details hidden ✓ Grouping Operations: Related operations grouped together ✓ Decoupling: Clients independent from subsystem ✓ Layering: Creates architectural layers
| Facade | Adapter |
|---|---|
| Simplifies interface | Makes incompatible interfaces work |
| Hides subsystem | Wraps single object |
| Makes easy things easier | Makes incompatible things compatible |
| Design-time decision | Runtime decision |
| Facade | Decorator |
|---|---|
| Simplifies complex subsystem | Adds behavior to single object |
| One-to-many | One-to-one |
| Hide subsystem | Enhance single object |
| Architectural pattern | Structural pattern |
// Java's Collections framework
Collections.sort(list); // Facade for sorting
Collections.unmodifiableList(l); // Facade for protection
// JDBC - Database Facade
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
// SLF4J - Logging Facade
Logger logger = LoggerFactory.getLogger(MyClass.class);
logger.info("Message");
// Spring's RestTemplate - HTTP Facade
restTemplate.getForObject(url, String.class);- Smart Home System: Single interface for lights, AC, security
- Movie Booking System: Book tickets, select seats, process payment
- Restaurant System: Order, prepare, deliver operations
- Banking System: Deposit, withdraw, transfer operations
- Car Dashboard: Engine, transmission, fuel displayed simply
- Web Frameworks: Simple APIs hiding HTTP complexity
- Database Access: ORM hiding SQL complexity
Hotel Reception Desk (Facade):
- Guest (Client) asks receptionist for everything
- Receptionist (Facade) coordinates:
- Room Service (subsystem)
- Housekeeping (subsystem)
- Front Desk (subsystem)
- Maintenance (subsystem)
- Guest doesn't need to know all departments
- Receptionist provides simple, unified interface
✓ Reduces complexity for clients ✓ Improves code readability ✓ Makes system easier to understand ✓ Loosens coupling with subsystems ✓ Makes changes to subsystem easier ✓ Provides single point of change
// Bad Facade - Too many methods
public class BadFacade {
public void operation1() { }
public void operation2() { }
public void operation3() { }
public void operation4() { }
public void operation5() { }
public void operation6() { }
public void operation7() { }
public void operation8() { }
// Not really simplifying anymore!
}
// Good Facade - Related operations grouped
public class GoodFacade {
public void goodMorning() { } // Related operations
public void leavingHome() { } // Related operations
public void comeBack() { } // Related operations
}HomeAutomationFacade.java- Smart home with unified control interfaceMovieTicketingFacade.java- Movie booking system facade