Structural Pattern
Facade Pattern
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
8 min readHigh Priority
1What is Facade Pattern?
Real-World Analogy
Hotel Concierge
Instead of dealing with housekeeping, restaurant, taxi, and tour services separately, you just tell the concierge what you need. They handle all the complex coordination behind the scenes.
Computer Power Button
One button starts CPU, memory, disk, BIOS, OS - a complex boot sequence. The power button is a facade hiding all that complexity.
Facade provides a simplified interface to a complex subsystem. It doesn't add functionality - it just makes the existing functionality easier to access by hiding the complexity.
Facade Pattern Structure
Facade provides simple interface while delegating work to subsystem classes.
2Facade vs Other Patterns
| Pattern | Intent | Interface |
|---|---|---|
| Facade | Simplify complex subsystem | New simpler interface |
| Adapter | Make incompatible interfaces work | Convert existing interface |
| Decorator | Add behavior dynamically | Same interface, enhanced |
| Proxy | Control access to object | Same interface |
3Implementation
Facade Pattern - Home Theater System
// ========== Complex Subsystem Classes ==========
class Amplifier {
public void on() { System.out.println("Amplifier on"); }
public void off() { System.out.println("Amplifier off"); }
public void setVolume(int level) { System.out.println("Volume: " + level); }
public void setSurroundSound() { System.out.println("Surround sound on"); }
}
class DVDPlayer {
public void on() { System.out.println("DVD Player on"); }
public void off() { System.out.println("DVD Player off"); }
public void play(String movie) { System.out.println("Playing: " + movie); }
public void pause() { System.out.println("Paused"); }
public void stop() { System.out.println("Stopped"); }
}
class Projector {
public void on() { System.out.println("Projector on"); }
public void off() { System.out.println("Projector off"); }
public void wideScreenMode() { System.out.println("Widescreen mode"); }
}
class Lights {
public void dim(int level) { System.out.println("Lights dimmed to " + level + "%"); }
public void on() { System.out.println("Lights on"); }
}
class Screen {
public void down() { System.out.println("Screen lowered"); }
public void up() { System.out.println("Screen raised"); }
}
// ========== Facade ==========
class HomeTheaterFacade {
private Amplifier amp;
private DVDPlayer dvd;
private Projector projector;
private Lights lights;
private Screen screen;
public HomeTheaterFacade(Amplifier amp, DVDPlayer dvd,
Projector proj, Lights lights, Screen screen) {
this.amp = amp;
this.dvd = dvd;
this.projector = proj;
this.lights = lights;
this.screen = screen;
}
// Simple unified interface
public void watchMovie(String movie) {
System.out.println("\n=== Get ready to watch a movie ===");
lights.dim(10);
screen.down();
projector.on();
projector.wideScreenMode();
amp.on();
amp.setVolume(5);
amp.setSurroundSound();
dvd.on();
dvd.play(movie);
}
public void endMovie() {
System.out.println("\n=== Shutting down ===");
dvd.stop();
dvd.off();
amp.off();
projector.off();
screen.up();
lights.on();
}
}
// ========== Client Code ==========
public class Main {
public static void main(String[] args) {
// Create subsystem components
Amplifier amp = new Amplifier();
DVDPlayer dvd = new DVDPlayer();
Projector proj = new Projector();
Lights lights = new Lights();
Screen screen = new Screen();
// Create facade
HomeTheaterFacade homeTheater = new HomeTheaterFacade(
amp, dvd, proj, lights, screen);
// Simple interface - one method does it all!
homeTheater.watchMovie("Inception");
// Later...
homeTheater.endMovie();
}
}Output:
=== Get ready to watch a movie === Lights dimmed to 10% Screen lowered Projector on Widescreen mode Amplifier on Volume: 5 Surround sound on DVD Player on Playing: Inception
4Real-World Applications
Spring JdbcTemplate
Simplifies JDBC operations - connection, statement, result handling in one call.
SLF4J Logger
Simple logging interface hiding Log4j, Logback complexity.
Compiler
compile() method hides lexer, parser, optimizer, code generator.
Payment Processing
ProcessPayment() hides validation, fraud check, gateway, notification.
5Interview Follow-up Questions
Interview Follow-up Questions
Common follow-up questions interviewers ask
6Key Takeaways
1Facade provides a simple interface to a complex subsystem.
2It coordinates subsystem calls but doesn't add behavior.
3Clients can still access subsystem directly if needed.
4Different from Adapter (simplifies vs converts interface).
5Common in: compilers, frameworks, payment systems, ORMs.
6Promotes loose coupling between clients and subsystems.