Façade Design Pattern in Java

In this article, we will discuss :
 
– What is Facade design pattern ?
– How to implement Facade design pattern in java
– Advantages of Facade pattern
– How is Facade pattern different from Adapter pattern ?
 

Facade Design Pattern

 
A Facade is used when an easier or simpler interface to an underlying object is desired. (wiki)
Facade
 
Facade pattern provides a simplified interface to a set of interfaces in a subsystem.

 

Facade pattern implementation in Java

 
In this example, our goal is to build cars.

For simplicity, we are assuming that it involves 3 tasks :

– adding engine
– adding a body and
– adding accessories

Lets create the Java classes for the same :
 

Add Engine

 

package com.topjavatutorial.patterns.facade;

public class CarEngine {

  public void setEngine(){
    System.out.println("Setting Car engine");
  }
}


 

Add Body

 

package com.topjavatutorial.patterns.facade;

public class CarBody {
  public void setBody() {
    System.out.println("Setting Car Body");
  }
}


 

Add accessories

 

package com.topjavatutorial.patterns.facade;

public class CarAccessories {
  public void setAccessories() {
    System.out.println("Setting Car Accessories");
  }
}

Now, lets add a CarFacade class that will provide a simpler interface for all these operations.
 

Unified Car Facade

 

package com.topjavatutorial.patterns.facade;

public class CarFacade {
  
  CarEngine engine;
  CarBody body;
  CarAccessories accessories;
  
  public CarFacade(){
    engine = new CarEngine();
    body = new CarBody();
    accessories = new CarAccessories();
  }
  
  public void buildCar(){
    System.out.println("Build car started");
    engine.setEngine();
    body.setBody();
    accessories.setAccessories();
    System.out.println("Build car completed");
  }

}


 

Client to test our Facade implementation

 

package com.topjavatutorial.patterns.facade;

public class FacadePatternDemo {

  public static void main(String[] args) {
    CarFacade facade = new CarFacade();
    facade.buildCar();
  }
}

Notice, how much simpler it is for the client compared to calling all the individual operations here.
 

Output of running client code


Build car started
Setting Car engine
Settting Car Body
Setting Car Accessories
Build car completed

 

Advantages of Facade Pattern

 
Facade pattern allows loose coupling between client and subsystems.

It defines a high level interface that makes using the subsystems easier.

 

Difference between Adapter and Facade pattern

 
Adapter pattern alters an interface so that it matches what the client is expecting.

The aim of Facade pattern is to provide a simplified interface to a subsystem.
 

Facade pattern uses

Façade pattern is commonly used in following scenarios:

– To provide interface to legacy backend system
– To reduce network calls : Façade makes many calls to subsystems, but remote client makes only one call to façade
– To encapsulate flow and inner details for data security
 

Further reading

 
Adapter pattern

Decorator Pattern

Observer Pattern

Singleton Design Pattern
 

Reference books

 
Head First Design Patterns

Head First Object-Oriented Analysis and Design

 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags