Design Pattern means that “we had some problem then we found the solution and gave name to this solution” and facade is also one of the solution so we need to find the problem that facade has solved and how ?
This design pattern comes under Structural Category .
================================================================
C++ Facade Design Pattern:
Standard Definition:
Provide a unified interface to a set of interfaces in subsystem. It defines a higher level that makes the subsystem easier to use.
In layman language:
First understand the facade meaning: “the principal front of a building, that faces on to a street or open space” so this is the main gate where we can do entry in building, this is the only way communication for outsider. Technical meaning “New Extra Class” and this will take care of all communication with sub systems.
Problem:
Client is tightly coupled with sub system and client needs to call many API of subsystem(It contains many classes) .
Solution:
Create one class named as facade, it will have new interface that is simple and unified, this will hide all the complicity and this solution called as “facade design pattern“. In other word, now client will communicate with facade only.
When we will use facade design pattern:
- When client is tightly coupled with subsystem and many interface are being used or called by client.
- There is possibility of change in subsystem, if any change happen in subsystem then client might need to change.
Advantage:
- After introducing Facade, now client knows only facade so if any change happen in subsystem this will not impact client.
- Facade class will hide all the complicity and it will provide simple interface to client/clients.
Key Points:
- Facade pattern follows “Dependency Inversion” principal because client is dependent on facade and it acts like abstraction layer.
- Facade class should have “Single responsibility” of providing the simple interface to client, I mean we should not add other functionality in it.
- Facade is Single point of contact for client/clients.
- Subsystem classes don’t have reference of facade so they don’t know about it, in other way reverse communication is not possible.
- Usually Facade is implemented as “Singleton” class.
- Instantiation of subsystem class can be handled in Facade class or client can pass the objects as parameter.
- Abstract factory can be used to create subsystem objects.
- Abstract factory can be used alternative of facade.
- Mediator is similar to Facade but it is different because reverse communication is not possible(from subsystem to client).
Class Diagram without Facade:
Class Diagram with Facade:
Source code with real example:
/* Program: Facade Design Pattern Here, lets assume Washing machine is one sub system and UI is client so Facade is single point of contact for sub system from UI Author: Alpha Master Date: 29 December 2022 */ //Header File #include<iostream> #include<thread> #include<chrono> class WashingMotor { public: void start(){std::cout<<"Motor Start"<<std::endl;} void stop(){std::cout<<"Motor Stop"<<std::endl;} }; class PreChecking { public: bool IsDoorClosed(){std::cout<<"Door is closed"<<std::endl;return true;} bool IsWaterLevel(){std::cout<<"Water Level Correct"<<std::endl;return true;} }; class facade { WashingMotor mMotor; PreChecking mPreCheck; public: static facade* getInstance() { static facade m_pinstance; return &m_pinstance; } void start() { mPreCheck.IsDoorClosed(); mPreCheck.IsWaterLevel(); mMotor.start(); } void stop(){mMotor.stop();} }; int main() { std::cout<<"Facade Design Pattern"<<std::endl; //Assume this is Client facade::getInstance()->start(); std::this_thread::sleep_for(std::chrono::seconds(1)); facade::getInstance()->stop(); //facade is sigle point of contact. //facade provided the simple interface to client. //client knows only facade. return 0; }
Output:
Facade Design Pattern Door is closed Water Level Correct Motor Start Motor Stop