Facade Design Pattern

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We are going to study about facade design pattern in this article. It is a structural design pattern and helps client applications to interact with system in easier way. It provides an interface for the client to access a system. We will look into it point wise in detail.

Facade Design Pattern:

The Gang of Four book defines facade design pattern as below

The facade design pattern provides a unified interface to a set of interfaces in subsystem. Facade defines a higher level interface that makes subsystem easier to use.

Necessity of Facade Design Pattern:

Bike Class First, let us try to create a simple application where a Rider rides a Bike,where both are classes. Bike class has multiple methods which perform certain function for a bike and Rider can invoke them. Class diagram for Bike class is as shown beside.

  • In general, for riding a bike, sitOnBike(), removeStand(), checkFuelTank(), startIgnition(), startEngine(), cluthAndGearUp() and twistThrottle() methods need to be invoked in order. It means rider is riding bike properly.
  • To stop the bike, applyBrakes(), closeThrottle(), cluthAndGearDown(), stopEngine(), killIgnition(), putBikeOnStand() and getOffBike() need to be invoked serially by rider.

Let us try simple program.

Bike.java

Rider.java

When you run the program, you will get output as below.

Sitting on bike now
Stand removed
Fuel tank full, good to go
Bike key inserted, Ignition on
Engine started
Shifted up
Giving full throttle now
Brakes applied
Throttle closed
Shifted down
Engine stopped
Ignition off, bike key removed
Put bike on stand
Got off bike

Points to notice here,

  • Rider class needs to know the order in which bike methods need to be called in order to run it properly.
  • Rider can have a simpler way in which he can start or stop the bike, right now he is calling bike methods directly and it is difficult.
  • We can make some intermediate class which simplifies Rider’s task of performing bike operations. A Rider only needs to worry about riding.

so to resolve this, we can create a BikeFacade and create two methods in it, startBike() and stopBike(), which perform actual tasks. Class diagram will be as below

Bike with BikeFacade

 

BikeFacade.java

This is our Facade Design Pattern. We have applied it to simply client code. Now Rider class becomes as below

This gives same output as mentioned above. We have successfully applied Facade Design Pattern. Points to note here

  • Rider does not need to know the order in which bike operations needs to be performed.
  • Rider simply calls startBike() from BikeFacade which takes care of the order i.e. implementation details.
  • Even if any changes are made in the order operations are performed, Rider need not care.

Benefits of Facade Design Pattern:

  • Reduction in coupling because client knows nothing about subsystem.
  • Increased maintainability when changes are required
  • If network calls are involved, and facade is called from remote client, then very few calls are needed as compared to directly calling business objects. This helps in boosting performance.

Although Facade Design Pattern looks like encapsulation, it is not hiding anything from client. Client can still access business objects directly. Facade design pattern simply provides a simpler way to access them.

I hope this helped understand facade design pattern. From next article onward we will see java EE specific implementation for design patterns.

Share Button

1 comment for “Facade Design Pattern

  1. July 21, 2016 at 9:59 pm

    Well explained! Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *