You are currently viewing Facade Design Pattern in Java

Facade Design Pattern in Java

What is the Facade Pattern?

The Facade Pattern is a structural design pattern that provides a simplified interface to a complex subsystem. It hides the complexities of the system and provides a cleaner and easier-to-use API to the client.

Intent

  • Provide a unified, high-level interface to a set of interfaces in a subsystem.
  • Make the subsystem easier to use.

When to Use

  • When you want to simplify interaction with complex systems.
  • When you want to reduce dependencies between client code and subsystems.
  • When refactoring large legacy systems.

Components

  1. Subsystem Classes – Complex classes with various functions.
  2. Facade Class – Wraps the subsystem and exposes only necessary functionality.
  3. Client – Uses the facade instead of calling subsystem classes directly.

Java Example

Imagine a Home Theater System with multiple components.

Step 1: Subsystem Classes

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

class DVDPlayer {
    public void on() { System.out.println("DVD Player ON"); }
    public void play(String movie) { System.out.println("Playing movie: " + movie); }
}

class Projector {
    public void on() { System.out.println("Projector ON"); }
    public void wideScreenMode() { System.out.println("Projector in widescreen mode"); }
}

Step 2: Facade Class

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

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

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

Step 3: Client Code

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

        HomeTheaterFacade homeTheater = new HomeTheaterFacade(amp, dvd, projector);
        homeTheater.watchMovie("Inception");
    }
}

Output

Get ready to watch a movie...
Amplifier ON
Volume set to 5
Projector ON
Projector in widescreen mode
DVD Player ON
Playing movie: Inception

Summary

AspectDescription
Pattern TypeStructural
Main GoalSimplify interaction with complex systems
Real-Life ExamplesCar dashboard, TV remote, ATM interface

Benefits

  • Simplifies client usage.
  • Reduces dependency on subsystem internals.
  • Promotes loose coupling.

Caution

  • Facade shouldn’t become a “God Object” doing too much.
  • Clients should still be able to access subsystem classes directly if needed.

This pattern is perfect when you want to decouple your code from implementation details or legacy complexity.

Leave a Reply