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.
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.