🧠 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! 💪