DEV Community

Cover image for Facade Design Pattern in Java
Matheus Bernardes Spilari
Matheus Bernardes Spilari

Posted on

Facade Design Pattern in Java

When working with object-oriented design, it's common to face complex systems composed of many interdependent classes. Sometimes, you just want a simple way to use them without worrying about the internal details.

This is where the Facade Pattern comes in.

What is the Facade Pattern?

The Facade Pattern provides a unified and simplified interface to a set of interfaces in a subsystem. It helps hide the complexities of the subsystem from the client and provides a higher-level interface that makes the subsystem easier to use.

It’s like having a remote control for your home theater — instead of turning on each device one by one, adjusting the settings, and starting the movie manually, you just press a single button.

Let’s bring this analogy to code.


Example: Home Theater System in Java

We’re going to simulate a simple home theater system using three components: an amplifier, a projector, and a DVD player. Then, we’ll create a facade that makes it easy to start and stop a movie night.


1. Subsystems – Complex Components

// Amplifier
public class Amplifier {
    public void on() {
        System.out.println("Amplifier turned on.");
    }

    public void off() {
        System.out.println("Amplifier turned off.");
    }

    public void setVolume(int level) {
        System.out.println("Volume set to " + level);
    }
}

// Projector
public class Projector {
    public void on() {
        System.out.println("Projector turned on.");
    }

    public void off() {
        System.out.println("Projector turned off.");
    }
}

// DVD Player
public class DvdPlayer {
    public void on() {
        System.out.println("DVD Player turned on.");
    }

    public void play(String movie) {
        System.out.println("Playing movie: " + movie);
    }

    public void off() {
        System.out.println("DVD Player turned off.");
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Facade Class – Simplifying the Interface

public class HomeTheaterFacade {
    private Amplifier amp;
    private Projector projector;
    private DvdPlayer dvd;

    public HomeTheaterFacade(Amplifier amp, Projector projector, DvdPlayer dvd) {
        this.amp = amp;
        this.projector = projector;
        this.dvd = dvd;
    }

    public void watchMovie(String movie) {
        System.out.println("Get ready to watch a movie...");
        amp.on();
        amp.setVolume(5);
        projector.on();
        dvd.on();
        dvd.play(movie);
    }

    public void endMovie() {
        System.out.println("Shutting down movie theater...");
        dvd.off();
        projector.off();
        amp.off();
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Client Code – Using the Facade

public class Main {
    public static void main(String[] args) {
        Amplifier amp = new Amplifier();
        Projector projector = new Projector();
        DvdPlayer dvd = new DvdPlayer();

        HomeTheaterFacade homeTheater = new HomeTheaterFacade(amp, projector, dvd);
        homeTheater.watchMovie("The Lord of the Rings");
        homeTheater.endMovie();
    }
}
Enter fullscreen mode Exit fullscreen mode

Benefits of the Facade Pattern

  • Simplifies usage of complex systems
  • Encapsulates interactions between subsystems
  • Improves readability and usability for clients
  • Helps promote loose coupling

Conclusion

The Facade pattern is a great tool when you want to provide a cleaner API over a set of complex classes. In real-world applications, this might mean creating a simplified interface for a library, an SDK, or even wrapping legacy code.

Whenever you find yourself writing repeated setup/teardown code that spans multiple components, consider wrapping it in a Facade.

Let the client press one button and enjoy the show.


📍 Reference

💻 Project Repository

👋 Talk to me

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.