DEV Community

Sota
Sota

Posted on

Facade Pattern

What is Facade Pattern?

Facade pattern is a structural pattern that provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

When to use it?

Use Facade pattern when you need simplified interface that orchestrates many classes and does the complex work.

Problem

Our company is going to sell a car. To start driving, a client needs to follow these procedure...

  1. unlock the door
  2. open the door
  3. start engine
  4. turn on the dashboard
  5. turn on the navigation screen
  6. if outside is dark, turn on the headlight
  7. close the door
  8. lock the door

Even a client hasn't pedal yet, there are already 8 steps.
How would it be in OOP? In fact, we need 5 different classes and 8 methods calls.

        door.unlock();
        door.open();
        engine.start();
        dashBoard.on();
        screen.on();
        if (isOutsideDark) {
            light.on();
        }
        door.close();
        door.lock();
Enter fullscreen mode Exit fullscreen mode

The question is, is there a easier way for a client to deal with tasks such as starting driving, stopping driving and so on? Yes, there is Facade pattern.

Solution

Image description

  1. Client
    Client can use CarFacade to do a task, and have access to subsystem classes.

  2. CarFacade
    Provides simplified interface to start driving, stop driving.

  3. Subsystem classes
    Complex system that hard to use. (For the sake of simplicity, I implemented not much complex subsystem)

Structure

Image description

Implementation in Java

Implementing subsystem classes is not important to explain Facade pattern, so I omit these parts but you can check in my Github at the end of this blog.

public class CarFacade {

    private DashBoard dashBoard;
    private Door door;
    private Engine engine;
    private HeadLight light;
    private NavigationScreen screen;

    private boolean isOutsideDark;

    public CarFacade(DashBoard dashBoard,
                     Door door,
                     Engine engine,
                     HeadLight light,
                     NavigationScreen screen,
                     boolean isOutsideDark) {
        this.dashBoard = dashBoard;
        this.door = door;
        this.engine = engine;
        this.light = light;
        this.screen = screen;
        this.isOutsideDark = isOutsideDark;
    }

    public void startDriving() {
        door.unlock();
        door.open();
        engine.start();
        dashBoard.on();
        screen.on();
        if (isOutsideDark) {
            light.on();
        }
        door.close();
        door.lock();
    }

    public void endDriving() {
        light.off();
        screen.off();
        dashBoard.off();
        engine.stop();
        door.unlock();
        door.open();
    }
}
Enter fullscreen mode Exit fullscreen mode
public class Client {

    public static void main(String[] args) {
        // Create components
        DashBoard dashBoard = new DashBoard();
        Door door = new Door();
        Engine engine = new Engine();
        HeadLight light = new HeadLight();
        GPS gps = new GPS();
        NavigationScreen screen = new NavigationScreen(gps);

        // Instantiates Facade with all the components in the subsystem
        CarFacade automatedCar = new CarFacade(dashBoard, door, engine, light, screen, true);

        // Use the simplified interface to start and end driving
        automatedCar.startDriving();
        System.out.println();
        automatedCar.endDriving();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Unlock the door
Open the door
Start Engine
DashBoard displays speed: 60km/h, fuel remaining: 70%
Navigation screen displays gps: 43.01787, -76.216435
HeadLight is on
Close the door
Lock the door

HeadLight is off
Navigation screen is off
DashBoard is off
Stop Engine
Unlock the door
Open the door
Enter fullscreen mode Exit fullscreen mode

Pitfalls

  • There is no really known pitfall to Facade.

Comparison with Adapter Pattern

  • Adapter pattern introduces new interface to adapt incompatible interface to the interface the client expects, while Facade pattern creates unified interface that makes subsystem easier to use.

You can check all the design pattern implementations here.
GitHub Repository

Top comments (0)