What is the facade pattern?
The Facade Pattern provides a simplified interface to a complex subsystem.
Think of it as a front desk in a hotel. Instead of interacting with the housekeeping, restaurant, laundry, and maintenance teams separately, you just call the reception desk. The reception (facade) hides the complexity and gives you a clean, unified interface.
🤔 Problem Without Facade
Imagine you are building a Home Theater System app. To play a movie, you might need to:
- Turn on the DVD player
- Start the projector
- Lower the screen
- Set up the sound system
Without a facade, a client would need to know the details of all these subsystems and their initialization sequence. This leads to tight coupling and unnecessary complexity.
✅ Solution With Facade
We create a Facade class (HomeTheaterFacade
) that provides one simple method, say watchMovie()
. Internally, this method coordinates all the subsystem operations.
Let's implement it in java
// Subsystems
class DVDPlayer {
public void on() { System.out.println("DVD Player ON"); }
public void play(String movie) { System.out.println("Playing " + movie); }
}
class Projector {
public void on() { System.out.println("Projector ON"); }
}
class Screen {
public void down() { System.out.println("Screen going down"); }
}
class SoundSystem {
public void on() { System.out.println("Sound System ON"); }
public void setVolume(int level) { System.out.println("Volume set to " + level); }
}
// Facade
class HomeTheaterFacade {
private DVDPlayer dvd;
private Projector projector;
private Screen screen;
private SoundSystem sound;
public HomeTheaterFacade(DVDPlayer dvd, Projector projector, Screen screen, SoundSystem sound) {
this.dvd = dvd;
this.projector = projector;
this.screen = screen;
this.sound = sound;
}
public void watchMovie(String movie) {
System.out.println("Get ready to watch a movie...");
screen.down();
projector.on();
sound.on();
sound.setVolume(10);
dvd.on();
dvd.play(movie);
}
}
// Client
public class FacadePatternDemo {
public static void main(String[] args) {
DVDPlayer dvd = new DVDPlayer();
Projector projector = new Projector();
Screen screen = new Screen();
SoundSystem sound = new SoundSystem();
HomeTheaterFacade homeTheater = new HomeTheaterFacade(dvd, projector, screen, sound);
homeTheater.watchMovie("your favorite movie");
}
}
Output
Get ready to watch a movie...
Screen going down
Projector ON
Sound System ON
Volume set to 10
DVD Player ON
Playing your favorite movie
The implementation may be overwhelming for beginners, therefore let's break it down what we just did.
High-Level Explanation of the Implementation
Subsystem Classes – These are the individual components (
DVDPlayer
,Projector
,Screen
,SoundSystem
). Each class has its own responsibilities, like turning on, playing a movie, or adjusting volume.Facade Class (
HomeTheaterFacade
) – This is the star of the show.
Instead of the client dealing with each subsystem directly, the facade bundles everything into one simple method:watchMovie()
. Behind the scenes, it calls the right methods in the correct sequence (lower screen → start projector → turn on sound → play DVD).Client Code (
FacadePatternDemo
) – The client only interacts with the Facade, not the subsystems. The complexity is hidden.
The client’s code is clean and easy to read: just call homeTheater.watchMovie("your favorite movie")
.
👉 The key takeaway:
The client doesn’t care how things work internally. It just uses one unified entry point (the facade), which takes care of coordinating the complex subsystem interactions.
Advantages
- Simplifies usage of complex subsystems
- Loose coupling between client and subsystem
- Makes code more readable and maintainable
Limitations
- Facade adds an extra layer, which may slightly increase overhead.
- If overused, it can become a "over burdened class" handling too much.
When to Use Facade Pattern?
- When you have a complex system with multiple subsystems.
- When you want to decouple clients from subsystem details.
- When you want to provide different entry points for different types of clients.
This is Part 7 of the Java Design Patterns Series.
If you find it insightful, please share your feedback. Also let me know if you have used facade pattern in your projects.
Next Up: Decorator Design Patterns!
Top comments (0)