text: Structural Pattern: Facade
  Structural Pattern: Facade
  Learn more about the structural pattern that reduces the complexity of your systems.
  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.
  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.
  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.
  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
  Published at DZone with permission of Yogen Rai , DZone MVB. See the original article here.
  Opinions expressed by DZone contributors are their own.
  {{ parent.title || parent.header.title}}
  {{ parent.tldr }}
  {{ parent.linkDescription }}{{ parent.urlSource.name }}