Facade Design Pattern

Before jumping on Facade software design pattern, we should know what facade really means.

In Civil engineering and architect world, definition of facade is given as :

The front of a building, especially an imposing or decorative one. Or

Any side of a building facing a public way or space and finished accordingly.

Usage of such an entry gate is useful because you will know exactly where to head towards. If you are visiting an aquarium and when your car approaches near it then do you start looking for entering a dolphin show directly using some unknown door or just look for entrance and take directions from there? We definitively look for entry gate and we know that once we hit this entry gate it will be vary clear how to access sections and shows within aquarium. At the entrance, all information is consolidated and readily accessible.

Now let's take another simple example: You are hosting a promotion success party. It means you got to do lots of work, visit lots of places or make lots of calls,follow ups, decide food menu, drinks menu, guests lists, format of invitations, emails, tracking RSVP, booking a place for party, buying various things for the party and so on... Instead of all this, if you just want to talk to one and only one person always and that person is event organizer. The face of your party, front gate to your party, one answer and solution to all your questions and problems. You no need to discuss with caterers, no need to sit and send hundreds of emails and invitations. Is event organizer really helpful? Takes of your headache and pain? I take your answer as YES :)

Now above examples is just to give a quick kick start to our topic. I believe it may help many beginners in understanding facade.

Now let's jump back to software world:

Let's take an application that consumes a large number of third-party services. In this case, the client needs to know about details such as the data formats, the service metadata, the exact way to invoke the individual services, and so on. These details might be overwhelming for the client code. Imagine having such interactions if required at many places in the client code.

In such cases it would help if the complexities were isolated from the client code. Instead of dealing with these subsystems (and hence the complex operations involved) the client code would invoke some easy-to-use component. The component in turn would take care of the complex interactions with one or more subsystems. This component would act as an entry point or front gate — the façade — for the client code. Thus the façade pattern provides a high-level, easy-to-use interface to the client by shielding the different interfaces of the subsystems .

The façade pattern makes the interactions with the subsystems easy as well as uniform, freeing the client from those details.

GoF Definition

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.

The façade pattern shields subsystem A, B and C by providing a high-level, uniform, and simplified interface. In the figure this interface takes the form of three methods —Operation1() , Operation2() , and Operation3() - of the Facade class.

Instead of interacting with the subsystems directly, the client invokes one or more of the methods provided by the façade and gets the job done.

Although the design of each subsystem might be different from the other subsystems, the client is shielded from these differences.

As a real world example, lets take a website. Aim of this website is to list down best prices of TVs(Teleivision) of all types and get their best prices from various sources and sellers.

Assume that each seller has exposed a webapi / webservice method through which we can query price and TV details. Some sellers have listed 20 features/parameters and some other sellers have put out some 30 different features/parameters for same model TV. So in short, there are various sub systems which needs to be interacted by your website and on top of it needs to compare and bring out the best price at that time.

This is good case to apply Facade pattern.