architecture

PatternMastery

Abstract Facade
Structural Pattern

The Facade Pattern

Hiding complexity behind a simple interface.

The Analogy: Think of a car starter button. When you push it, a complex sequence of events happens: the battery powers the starter motor, fuel is injected, sparks fire, and pistons move. As the driver, you don't need to manually trigger these systems; you just press one simple button. That button is the Facade.

Interactive Simulation: Home Theater

See how the Facade orchestrates complex subsystems. Try to set up "Movie Mode" manually, then try the Facade button.

Client Interface (The Facade)

smartphone

One click triggers the entire workflow.

EXECUTION LOG
// Waiting for command...

Complex Subsystems

The chaotic reality behind the facade.

settings_applications
Lights lightbulb
Power
Dimmer
Projector videocam
Power
Input NONE
Amplifier speaker
Power
Vol
Screen curtains
Position UP
Player smart_display
Status IDLE
Popcorn local_pizza
Popper

Implementation Breakdown


// Without Facade (Messy!)
// You have to manually call every single subsystem
lights.dim(10);
screen.down();
projector.on();
projector.setInput("HDMI");
amp.on();
amp.setVolume(5);
player.on();
player.play("The Matrix");

// ----------------------------------------------------

// WITH FACADE (Clean!)
// The client only interacts with the simple interface
const homeTheater = new HomeTheaterFacade(lights, screen, projector, amp, player);

homeTheater.watchMovie("The Matrix"); 
// ^ One line does it all!
class HomeTheaterFacade {
    constructor(lights, screen, projector, amp, player) {
        this.lights = lights;
        this.screen = screen;
        this.projector = projector;
        this.amp = amp;
        this.player = player;
    }

    watchMovie(movie) {
        console.log("Get ready to watch a movie...");
        this.lights.dim(10);
        this.screen.down();
        this.projector.on();
        this.projector.setInput("HDMI");
        this.amp.on();
        this.amp.setVolume(11);
        this.player.on();
        this.player.play(movie);
    }

    endMovie() {
        console.log("Shutting down movie theater...");
        this.lights.on();
        this.screen.up();
        this.projector.off();
        this.amp.off();
        this.player.stop();
        this.player.off();
    }
}
// These are the complex classes with many methods
// that the Facade hides from the client.

class Projector {
    on() { /* voltage logic */ }
    off() { /* cooling logic */ }
    tvMode() { ... }
    wideScreenMode() { ... }
    setInput(input) { ... }
}

class Amplifier {
    on() { ... }
    setStereoSound() { ... }
    setSurroundSound() { ... }
    setVolume(level) { ... }
}

// ... etc for Lights, Screen, DVDPlayer

Advantages & Disadvantages

thumb_up

Advantages

  • check_circle
    Simplified Interface

    Provides a simple entry point to a complex subsystem, making it easier for new developers to use.

  • check_circle
    Decoupling

    Decouples the subsystem from its clients. If the subsystem changes (e.g., new Projector library), you only modify the Facade, not every client.

  • check_circle
    Layering

    Helps layer your application. Facades can serve as entry points to each layer (Subsystem A -> Facade A -> Subsystem B).

thumb_down

Disadvantages

  • cancel
    The "God Object" Risk

    An Facade can become a "God Object" coupled to all classes of an app if not careful. It might become too complex itself.

  • cancel
    Limited Power

    Simplified interfaces often hide advanced features. Power users might need to bypass the Facade to access low-level functionality.

  • cancel
    Performance Overhead

    Adds an extra layer of method calls. Usually negligible, but in high-performance computing, every abstraction counts.