π§ What is the Facade Pattern?
The Facade Pattern is a structural design pattern that provides a unified interface to a set of interfaces in a subsystem. It hides the complexities of the system and provides a simple interface to the client.
π§’ Real-World Analogy
Imagine ordering pizza π. You donβt talk to the chef, the delivery guy, and the cashier separately. You call the front desk (facade), and they handle everything behind the scenes.
β When to Use the Facade Pattern
- To simplify complex systems.
- To decouple client code from the subsystem.
- To reduce dependencies between clients and subsystems.
π§± Structure
+----------+ +-------------------+
| Client |-----> | Facade |
+----------+ +-------------------+
| | |
v v v
+-----------+ +----------+ +-----------+
| Subsystem | | Subsystem| | Subsystem |
| Class A | | Class B | | Class C |
+-----------+ +----------+ +-----------+
π‘ Example: Home Theater System
Weβll simulate a Home Theater setup where the client only interacts with a HomeTheaterFacade
, while all the subsystems like DVDPlayer
, Projector
, and Lights
are handled internally.
β 1. Subsystems
public class DVDPlayer {
public void on() {
System.out.println("DVD Player ON");
}
public void play(String movie) {
System.out.println("Playing movie: " + movie);
}
public void off() {
System.out.println("DVD Player OFF");
}
}
public class Projector {
public void on() {
System.out.println("Projector ON");
}
public void wideScreenMode() {
System.out.println("Projector in widescreen mode");
}
public void off() {
System.out.println("Projector OFF");
}
}
public class Lights {
public void dim(int level) {
System.out.println("Lights dimmed to " + level + "%");
}
public void on() {
System.out.println("Lights ON");
}
}
β 2. The Facade Class
public class HomeTheaterFacade {
private DVDPlayer dvdPlayer;
private Projector projector;
private Lights lights;
public HomeTheaterFacade(DVDPlayer dvd, Projector projector, Lights lights) {
this.dvdPlayer = dvd;
this.projector = projector;
this.lights = lights;
}
public void watchMovie(String movie) {
System.out.println("\nGet ready to watch a movie...");
lights.dim(10);
projector.on();
projector.wideScreenMode();
dvdPlayer.on();
dvdPlayer.play(movie);
}
public void endMovie() {
System.out.println("\nShutting movie theater down...");
dvdPlayer.off();
projector.off();
lights.on();
}
}
β 3. Client Code
public class FacadePatternDemo {
public static void main(String[] args) {
DVDPlayer dvd = new DVDPlayer();
Projector projector = new Projector();
Lights lights = new Lights();
HomeTheaterFacade homeTheater = new HomeTheaterFacade(dvd, projector, lights);
homeTheater.watchMovie("Inception");
homeTheater.endMovie();
}
}
π§ͺ Output
Get ready to watch a movie...
Lights dimmed to 10%
Projector ON
Projector in widescreen mode
DVD Player ON
Playing movie: Inception
Shutting movie theater down...
DVD Player OFF
Projector OFF
Lights ON
π― Benefits of the Facade Pattern
β
Hides complexity
β
Makes the subsystem easier to use
β
Reduces the learning curve
β
Promotes loose coupling
π Where Itβs Used in Java
-
javax.faces.context.FacesContext
in Java EE -
java.net.URLConnection
β wraps multiple networking steps - Springβs
JdbcTemplate
β simplifies JDBC API
π§ Summary Table
Aspect | Description |
---|---|
Pattern Type | Structural |
Purpose | Simplify complex subsystems |
Key Class | Facade |
Real-world analogy | Hotel reception or pizza ordering |
Benefit | Loose coupling & easy client interaction |
π₯ Bonus Tip
Pair the Facade with other patterns like Adapter, Composite, or Decorator to architect enterprise-grade systems.
πΊοΈ UML Diagram (Text Format)
+-----------------------+
| HomeTheaterFacade |
+-----------------------+
| -dvdPlayer |
| -projector |
| -lights |
+-----------------------+
| +watchMovie(movie) |
| +endMovie() |
+-----------------------+
^ ^ ^
| | |
+------------+ +---------+ +--------+
| DVDPlayer | | Projector| | Lights|
+------------+ +---------+ +--------+
π¬ Let me know if you're ready for Day 8, or want to dive into:
- Chain of Responsibility
- State Pattern
- Strategy Pattern
Your design pattern mastery is leveling up big time! πͺ
Top comments (0)