Facade Pattern

Facade Pattern promotes decoupling the subsystem from its potentially many clients by encapsulating a complex subsystem within a single interface object. This reduces the communication and dependencies between the subsystems and reduces the complexity of the system.

Facade Pattern falls under structural patterns.

The intent of this pattern, according to Design Patterns by Gamma et al, are to

  • Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

 

Structure of Facade Pattern

Facade wraps a complicated subsystem with a simpler interface. The following figure shows the structure of facade pattern.

facade pattern structure

Figure : Facade Pattern Structure

Here, all the complexities are wrapped by facade. This is example where a poorly designed collection of APIs is wrapped with a single well-designed API.

One important thing to be noted is that there can more than one facade classes.

Here is another more practical example to illustrate the facade pattern.

facade-pattern-structure

Figure: Facade Pattern Structure

Here, participants are :

  • Facade: The facade class abstracts Packages 1, 2, and 3 from the rest of the application.
  • Clients: The objects are using the Facade Pattern to access resources from the Packages.

 

Example

Let us consider a system to create a car. There are so many complex processes, parts while creating a complete car. All those can be wrapped in a single facade class as shown in class diagram below.

facade-pattern-example

Figure: Car creating system

 

Java Implementation

First, let us create the proccess that are complex.

//complex parts of the system
public class CarBody {
    public void setCarBody(){
        System.out.println("Car body is set.");
    }
}
public class CarEngine {
    public void setCarEngine(){
        System.out.println("Car Engine is set.");
    }
}
public class CarAccessories {
    public void setCarAccessories(){
        System.out.println("Car accessories are set.");
    }
}

Now, lets wrap these complex processes into facade class.

/**
 * Facade class to wrap the complex parts
 */
public class CarFacade {
    private CarEngine engine;
    private CarBody body;
    private CarAccessories accessories;

    public CarFacade(){
        this.engine = new CarEngine();
        this.body = new CarBody();
        this.accessories = new CarAccessories();
    }

    public void createCar(){
        System.out.println("Creating a car...");
        engine.setCarEngine();
        body.setCarBody();
        accessories.setCarAccessories();
        System.out.println("Your car is ready.");
    }
}

Now, clients can use this facade to create the car without knowledge of underlying complexities.

/**
 * client
 */
public class Application {
    public static void main(String[] args) {
        CarFacade facade = new CarFacade();
        facade.createCar();
    }
}

The output of the program is:

Creating a car...
Car Engine is set.
Car body is set.
Car accessories are set.
Your car is ready.

 

Reference: Head First Design Patterns

Strategy Pattern
Template Method Pattern