Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Structural Pattern: Facade

DZone's Guide to

Structural Pattern: Facade

Learn more about the structural pattern that reduces the complexity of your systems.

· Java Zone ·
Free Resource

Atomist automates your software deliver experience. It's how modern teams deliver modern software.

The 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:

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 the facade pattern.

facade pattern structure

Figure 1: Facade Pattern Structure

Here, all the complexities are wrapped by the facade. This is an 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 2: 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 and parts while creating a complete car. All those can be wrapped in a single facade class as shown in the class diagram below.

facade-pattern-example

Figure 3: Car creating system

Java Implementation

First, let us create the process that is 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, let's wrap these complex processes into the 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 the 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.


You can see how the client doesn't care about the internal complexities of the car, rather the focus on its behavior.

Conclusion

This post talked about the summarized form of the Facade pattern, as one of the GOF patterns, with a simple example. 

The source code for all example presented above is available on GitHub.

Happy coding!

Reference: Head First Design Patterns

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!

Topics:
java ,design pattens ,facade ,facade pattern ,structural pattern ,code ,code example

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}