Java programming language logo

Facade Design Pattern

The Facade pattern provides a simplified interface to larger system. By this it hides the complexity of the system.

The Facade pattern can be applied when a complex library or framework is often used in the same way by its clients. For example, imagine a situation where a library has to be initialized in several steps to get ready. It can be error prone and not efficient for the clients. If there is a recommended order for those steps, it is better to offer a single method to the clients that arranges the whole initialization process and completely hides those steps away.

Moreover not just one, but several Facade can be implemented for a system.

Facade pattern is a structural pattern.

Example

In the below Java example, we are going to dock and undock a yacht. Operating a yacht is a complex task. Every time when the captain needs to undock, he starts the engine, pulls the boarding ramp up, unleashes the ropes and picks up the anchor. These steps are always the same, so instead of calling four methods every time, they should be hidden by a method called undock.

First of all, we create the four component of the yacht. Let’s start with the engine.


package com.programcodex.designpatterns.facade;

public class Engine {

    public void start() {
        System.out.println("Engine has been started.");
    }
    
    public void stop() {
        System.out.println("Engine has been stopped.");
    }    
}

The second class represents the boarding ramp.


package com.programcodex.designpatterns.facade;

public class BoardingRamp {

    public void letDown() {
        System.out.println("Boarding ramp has been let down.");
    }
    
    public void pullUp() {
        System.out.println("Boarding ramp has been pulled up.");
    }    
}

Third class is the rope’s class


package com.programcodex.designpatterns.facade;

public class Rope {

    public void tie() {
        System.out.println("Rope has been tied.");
    }
    
    public void unleash() {
        System.out.println("Rope has been unleashed.");
    }
}

The last component of our yacht is the anchor.


package com.programcodex.designpatterns.facade;

public class Anchor {

    public void pickUp() {
        System.out.println("Anchor has been picked up.");
    }
    
    public void drop() {
        System.out.println("Anchor has been dropped.");
    }
}

Finally, let's create the Yacht class, which holds the above components. It has two methods, the dock and the undock. These methods hide the complexity of the system.


package com.programcodex.designpatterns.facade;

public class Yacht {

    private final Engine engine;
    private final BoardingRamp boardingRamp;
    private final Rope rope;
    private final Anchor anchor;
    
    public Yacht(Engine engine, BoardingRamp boardingRamp, Rope rope, Anchor anchor) {
        this.engine = engine;
        this.boardingRamp = boardingRamp;
        this.rope = rope;
        this.anchor = anchor;
    }

    public void dock() {
        anchor.drop();
        rope.tie();
        engine.stop();
        boardingRamp.letDown();
        System.out.println("Yacht is docked.\n");
    }

    public void undock() {
        engine.start();
        boardingRamp.pullUp();
        rope.unleash();
        anchor.pickUp();
        System.out.println("Yacht is undocking.\n");
    }
}

And we are done. It’s time to run these things.

The Yacht with its components gets instantiated and its undock and dock methods are called.


package com.programcodex.designpatterns.facade;

public class TestFacade {

    public static void main(String[] args) {
        Engine engine = new Engine();
        BoardingRamp boardingRamp = new BoardingRamp();
        Rope rope = new Rope();
        Anchor anchor = new Anchor();
        
        Yacht yacht = new Yacht(engine, boardingRamp, rope, anchor);
        yacht.undock();
        yacht.dock();
    }
}

The output of the above test class:

The Facade Design Pattern