Facade Design Pattern

Facade Design Pattern is the Structural design patterns (like Adapter pattern and Decorator pattern). Facade design pattern is used to easily interact with the complex system.

According to Design Patterns: Elements of Reusable Object-Oriented Software (Gang of Four) 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.

Let’s understand with a simple example,

We have a holiday package booking application. We have 3 type of holiday packages available (Dimond, Gold & Silver)

A user can select the preferred destination and package, and based on the selected package and destination our application will start the booking process.

Booking Process includes:

  • Book return trip flight.
  • Book hotel
  • Book cabs for the complete holiday
  • Book a guide

In our example, we will implement first three booking facility.

There are 2 ways to implement this problem statement.

  1. let our client application trigger each booking one by one.

Problem with this approach

when we add or remove new service then we also have to make the change in the client application.

The client application needs to have all the details of our complex system.

WithoutFacade.png

   Adding new service need to change client application.

    1. What if we add an interface (*it is not a java interface) between client and services and let that interface execute the needed service. In this way, our client only has to interact with that interface and all other complex things are taken. We call this interface A FAÇADE Interface or FAÇADE Class.

WithFacade.png

Our FAÇADE class will take care of all the complexity so that our client application doesn’t have to worry about. He just has to trigger booking method in FAÇADE class.

Code Example:

Services

FlightBooking.java

HotelBooking.java

CabBooking.java

FAÇADE

HolidayBookingFacade

Client

Client.java

Output:

The Facade design pattern simplifies the interface to a complex system; because it is usually composed of all the classes which make up the subsystems of the complex system.

17 thoughts on “Facade Design Pattern”

  1. naturally like your website but you need to check the spelling on quite a few of your posts. Several of them are rife with spelling issues and I find it very bothersome to tell the truth nevertheless I’ll definitely come back again.

  2. Amazing! This blog looks exactly like my old one! It’s on a totally different topic but it has pretty much the same page layout and design. Great choice of colors!

  3. It’s really a nice and useful piece of information. I’m satisfied that you simply shared this useful information with us. Please keep us informed like this. Thank you for sharing.

Leave a Reply

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