DEV Community

Dev Cookies
Dev Cookies

Posted on

πŸ“ Day Seven: Facade Design Pattern in Java

🧠 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 |
              +-----------+ +----------+ +-----------+
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ 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");
    }
}
Enter fullscreen mode Exit fullscreen mode
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");
    }
}
Enter fullscreen mode Exit fullscreen mode
public class Lights {
    public void dim(int level) {
        System.out.println("Lights dimmed to " + level + "%");
    }

    public void on() {
        System.out.println("Lights ON");
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… 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();
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… 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();
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ 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
Enter fullscreen mode Exit fullscreen mode

🎯 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|
+------------+ +---------+ +--------+
Enter fullscreen mode Exit fullscreen mode

πŸ’¬ 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)