Hiding complexity behind a simple interface.
The Analogy: Think of a car starter button. When you push it, a complex sequence of events happens: the battery powers the starter motor, fuel is injected, sparks fire, and pistons move. As the driver, you don't need to manually trigger these systems; you just press one simple button. That button is the Facade.
See how the Facade orchestrates complex subsystems. Try to set up "Movie Mode" manually, then try the Facade button.
One click triggers the entire workflow.
The chaotic reality behind the facade.
// Without Facade (Messy!)
// You have to manually call every single subsystem
lights.dim(10);
screen.down();
projector.on();
projector.setInput("HDMI");
amp.on();
amp.setVolume(5);
player.on();
player.play("The Matrix");
// ----------------------------------------------------
// WITH FACADE (Clean!)
// The client only interacts with the simple interface
const homeTheater = new HomeTheaterFacade(lights, screen, projector, amp, player);
homeTheater.watchMovie("The Matrix");
// ^ One line does it all!
class HomeTheaterFacade {
constructor(lights, screen, projector, amp, player) {
this.lights = lights;
this.screen = screen;
this.projector = projector;
this.amp = amp;
this.player = player;
}
watchMovie(movie) {
console.log("Get ready to watch a movie...");
this.lights.dim(10);
this.screen.down();
this.projector.on();
this.projector.setInput("HDMI");
this.amp.on();
this.amp.setVolume(11);
this.player.on();
this.player.play(movie);
}
endMovie() {
console.log("Shutting down movie theater...");
this.lights.on();
this.screen.up();
this.projector.off();
this.amp.off();
this.player.stop();
this.player.off();
}
}
// These are the complex classes with many methods
// that the Facade hides from the client.
class Projector {
on() { /* voltage logic */ }
off() { /* cooling logic */ }
tvMode() { ... }
wideScreenMode() { ... }
setInput(input) { ... }
}
class Amplifier {
on() { ... }
setStereoSound() { ... }
setSurroundSound() { ... }
setVolume(level) { ... }
}
// ... etc for Lights, Screen, DVDPlayer
Provides a simple entry point to a complex subsystem, making it easier for new developers to use.
Decouples the subsystem from its clients. If the subsystem changes (e.g., new Projector library), you only modify the Facade, not every client.
Helps layer your application. Facades can serve as entry points to each layer (Subsystem A -> Facade A -> Subsystem B).
An Facade can become a "God Object" coupled to all classes of an app if not careful. It might become too complex itself.
Simplified interfaces often hide advanced features. Power users might need to bypass the Facade to access low-level functionality.
Adds an extra layer of method calls. Usually negligible, but in high-performance computing, every abstraction counts.