Facade is a structural design pattern. It defines a higher-level interface that makes the subsystem easier to use.
Facade means the mask (मुखौटा) and the pattern is nothing but a simplified interface to a more complex system by providing a wrapper for the sub-system.
Consider starting your car, let us see what happens under the hood when you turn the key (or press button) to start your car:
And your car is now Start. To do all this there are 3 sub-systems:
Imagine if there is no key and you have to start the engine yourself by doing all the above tasks.. It will be cumbersome.
The key is a FACADE which hides this complicated system of Car-Starting. The below class diagram is taken from the Facade page of wiki:
Facade design pattern
Another example of Facade is the customer service station.
The consumer calls one number and speaks with a customer service representative. The customer service representative acts as a Facade, providing an interface to multiple divisions.
Essentially in Facade, we are designing a wrapper class to encapsulate the functionality. It captures the collaboration of components and provides a simpler interface to the client.
Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
Example Code:
/** Facade Design Pattern. */ /** The 'Subsystem ClassA' class */ class SubSystemOne { public: void MethodOne() { cout<< " SubSystemOne Method"; } }; /** The 'Subsystem ClassB' class */ class SubSystemTwo { public: void MethodTwo() { cout<<" SubSystemTwo Method"; } }; /** The 'Subsystem ClassC' class */ class SubSystemThree { public: void MethodThree() { cout<<" SubSystemThree Method"; } }; /** The 'Facade' class (Wrapper) */ class Facade { private: SubSystemOne _one; SubSystemTwo _two; SubSystemThree _three; public: // Constructor Facade(); void MethodA() { cout<< "\nMethodA() ---- "; _one.MethodOne(); _two.MethodTwo(); } void MethodB() { cout<<"\nMethodB() ---- "; _two.MethodTwo(); _three.MethodThree(); } }; int Main() { Facade facade; facade.MethodA(); facade.MethodB(); // Wait for user getchar(); }