Python – The Facade Design Pattern

Share

Understanding facade design pattern

The term facade is referred as the principal front of a building, that faces on to a street or open space.When people pass by a facade they can view the exterior face but they will not be able to view the complexities within. This is how facade design pattern is implemented. Facade hides the inner complexities of the system and provides the simplest interface to the client.

Let us understand facade design pattern with a simple real world example.

Consider an example of a pharmacy. You will bring out certain a list of medicines from the prescription of the doctor. You will not be aware of the medicines that are placed in the pharmacy. Only the pharmacist will be able to point out the medicines and hand it over you. The customer doesn’t need to know the pharmacy. All the work is done by the simple interfacce i.e pharmacist.

Major Essentials of Facade design pattern

Provides a unified interdace to set of interfaces in a subsystem and defines a high level interface to help client use the subsystem efficiently.

Facade class collects all system classes, methods and provide required meaningful APIs so this way for client there will be minimum call. If any changes needs to be done in sub systems, there will not be any change for facade class and that way it will be loosely coupled.

Implementing the Facade pattern in real world

Let us assume you are planning to organize a software product launch event. It takes a whole lot of responsibility to well organize the event all by yourself. You need to book a hotel, a caterar for food arrangement, a florist to decorate the stage, and musical arrangments as well. Your first priority will be consulting to an event organizer. He will make sure that he will be resposible to talk to the individual services providers and get the best deal for you.

Let us look in the Facade design pattern’s perspective :

Client : It is you who needs to have a software product launch organized and completed in time.

Facade : The event organizer is responsible to talk with the service providers who can work for you to make arrangements.

Subsystems : They are the systems that provide services such as hotel management, flower decorations, caterine services and music arrangements.

Let us dive deeper implementing Facade design pattern with python.

Hotelier checks whether the hotel is available on the required date (__isAvailable) . Florist has setFlowerRequirements() method to set the type of flowers required for the event. Caterier is responsible for the food arrangements and has the setCuisine() method to accept the type of cuisine.abs Musician class is responsible to make music arrangements and has the setMusicTYpe() method.

In the above example wew can realte the Facade Pattern with the real world scenario in the following way:

1. EventOrganizer class is the Facade that doest the work for you and simpliefies the client (you).

2. EventOrganizer uses composition to create objects of the subsystems such as Hotelier, Caterer, Musician and Florist.

View the code as well as output in the github source:

https://github.com/kcsanjeeb/Python-design-patterns/blob/master/facade.ipynb

We will go through observer design pattern pattern in the next topic of design patterns in python. 

You may also like...

1 Response

  1. July 29, 2020

    […] will go through facade design pattern pattern in the next topic of design patterns in […]

Leave a Reply

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