DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Structural Pattern: Facade

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

Yogen Rai user avatar by
Yogen Rai
CORE ·
Jan. 08, 19 · Presentation
Like (11)
Save
Tweet
Share
15.42K Views

Join the DZone community and get the full member experience.

Join For Free

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

Facade pattern

Published at DZone with permission of Yogen Rai, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Handling Virtual Threads
  • Integration: Data, Security, Challenges, and Best Solutions
  • Explaining: MVP vs. PoC vs. Prototype
  • Internal Components of Apache ZooKeeper and Their Importance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: