Facade Design Pattern

29/01/2012

GoF definition for facade design pattern is, “Provide a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use.”

How do we infer the above definition? Think of a component that solves a complex business problem. That component may expose lot of interfaces to interact with it. To complete a process flow we may have to interact with multiple interfaces.

To simplify that interaction process, we introduce facade layer. Facade exposes a simplified interface (in this case a single interface to perform that multi-step process) and internally it interacts with those components and gets the job done for you. It can be taken as one level of abstraction over an existing layer.

Facade design pattern is one among the other design patterns that promote loose coupling. It emphasizes one more important aspect of design which is abstraction. By hiding the complexity behind it and exposing a simple interface it achieves abstraction.

Real World Examples for Facade Pattern

I wish to give you couple of real world examples. Lets take a car, starting a car involves multiple steps. Imagine how it would be if you had to adjust n number of valves and controllers. The facade you have got is just a key hole. On turn of a key it send instruction to multiple subsystems and executes a sequence of operation and completes the objective. All you know is a key turn which acts as a facade and simplifies your job.

Similarly consider microwave oven, it consists of components like trasnformer, capacitor, magnetron, waveguide and some more. To perform an operation these different components needs to be activated in a sequence. Every components has different outputs and inputs. Imagine you will have separate external controller for all these components using which you will heat the food. It will be complicated and cumbersome.

In this scenario, oven provides you preprogrammed switches which can be considered as a facade. On click on of a single switch the job gets done. That single menu switch works as an abstraction layer between you and the internal components.

These are realworld examples for facade design pattern. In software scenario, you can have interfaces which acts as a facade. Methods in these interfaces contains the interaction sequence, formatting and converting data for input for components. As such it will not hold the business logic.

UML Diagram for Facade Design Pattern

Common Mistakes while Implementing Facade Design Pattern

In my experience the common mistakes I have seen is,

  • just for the sake of introducing a facade layer developers tend to create additional classes. Layered architecture is good but assess the need for every layer. Just naming a class as ABCDFacade.java doesn’r really make it a facade.
  • Creating a java class and ‘forcing’ the UI to interact with other layers through it and calling it a facade layer is one more popular mistake. Facade layer should not be forced and its always optional. If the client wishes to interact with components directly it should be allowed to bypass the facade layer.
  • Methods in facade layer has only one or two lines which calls the other components. If facade is going to be so simple it invalidates its purpose and clients can directly do that by themselves.
  • A controller is not a facade.
  • Facade is ‘not’ a layer that imposes security and hides important data and implementation.
  • Don’t create a facade layer in advance. If you feel that in future the subsystem is going to evolve and become complicated to defend that do not create a stub class and name it a facade. After the subsystem has become complex you can implement the facade design pattern.
  • Subsystems are not aware of facade and there should be no reference for facade in subsystems.

Summary of Facade Design Pattern

  • Facade provides a single interface.
  • Programmers comfort is a main purpose of facade.
  • Simplicity is the aim of facade pattern.
  • Facade design pattern is used for promoting subsystem independence and portability.
  • Subsystem may be dependent with one another. In such case, facade can act as a coordinator and decouple the dependencies between the subsystems.
  • Translating data to suit the interface of a subsystem is done by the facade.

Facade Vs Mediator Design Pattern

Mediator design pattern may look very similar to facade design pattern in terms of abstraction. Mediator abstracts the functionality of the subsystems in this way it is similar to the facade pattern. In the implementation of mediator pattern, subsystem or peers components are aware of the mediator and that interact with it. In the case of facade pattern, subsystems are not aware of the existence of facade. Only facade talks to the subsystems.

Facade Design Pattern in Java API

ExternalContext behaves as a facade for performing cookie, session scope and similar operations. Underlying classes it uses are HttpSession, ServletContext, javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse.

Good article man, good examples and key points.
Keep it going ;)

Alex on January 29th, 2012 8:55 pm

Clear :)

Kat on January 30th, 2012 11:59 am

very nice article!
It Consists all the things like common mistakes, api example and real world example.. :)

Keep growing..

Rajesh on January 30th, 2012 12:05 pm

Is facade is similar to the concept ” Code for interface not for implementation”

Ahmad on January 30th, 2012 12:14 pm

How is it different from factory?

Suman on January 30th, 2012 12:21 pm

Very Good Article

Sudheer on January 30th, 2012 12:58 pm

Good article.

Itudoben on January 30th, 2012 6:32 pm

Very good article… Keep going …

Chaitanya on January 30th, 2012 7:07 pm

nice article sir….

Anonymous on January 30th, 2012 8:13 pm

Like your way of explanation with real world example. Nice article.

Dhiren on January 30th, 2012 11:37 pm

Please explain coupling and cohesion

cndhu on January 31st, 2012 10:08 am

your way of explanation and view is very nice. Please provide programmatic explanation.

Anonymous on January 31st, 2012 3:01 pm

excellent!!

Suyog on February 2nd, 2012 2:45 am

nicly explained but can any body give the code for the same

Anonymous on February 7th, 2012 3:45 pm

greate example and keypoints

Manoj Dwivedi on February 10th, 2012 1:03 pm

@Ahmad, this is not an implementation of code for interface.

I will try to add some code example soon.

Joe on February 12th, 2012 7:21 pm

nice artical… :)

indrajeet on February 17th, 2012 3:11 pm

can we able to get…application codes…?
in case if we want code for some module to design an application…in java…
is there any link or site available which will help us in such case??

indrajeet on February 17th, 2012 3:15 pm

Thanks for the above details. could you please explain JDO in java.

Sidharth on March 1st, 2012 8:22 pm

Design Pattern are explained very clearly and pointed out key areas..thanks

santosh on March 2nd, 2012 8:34 am

very nice article. IT will be more helpful if u add java code demonstrating FACADE

Anonymous on March 12th, 2012 1:20 pm

Very good Article
Keep it going :)

Ranjit on March 15th, 2012 1:05 pm

nice examples giving clear roadmap of design pattern

trim on March 16th, 2012 11:00 am

nice article

prity on March 26th, 2012 6:30 pm

hi joe,
it’s a nice article can u provide this with small example.

sivakumar on March 27th, 2012 11:39 am

Excellent!!

Simple and relevant information.

Naveen on March 29th, 2012 2:18 pm

Excellent. Very clear concept

Abir on April 5th, 2012 10:05 pm

Very nice and well explained! Thanks!

Rodolfo Guzman on May 8th, 2012 5:26 pm

clear explanation

prasanti on May 15th, 2012 4:06 pm

excellent job done

Anonymous on May 24th, 2012 2:47 am

Excellent. Very clear

Anonymous on May 30th, 2012 2:46 pm

very helpful, thanks a lot !

Anonymous on May 31st, 2012 8:39 pm


Email:

about
I am Joe, author of this blog. I run this with loads of passion. If you are into java, you may find lot of interesting things around ...more about me. Google+
java badge
Home