What is the facade design pattern?
Overview
The facade is a design pattern from software development that belongs to the category of structural patterns.
Purpose
This structural pattern gives a complicated set of classes, libraries, or frameworks a simple interface. While facade reduces the overall complexity of the program, it also aids in the consolidation of unneeded dependencies.
Benefits
-
It improves the readability and usefulness of a software library. It does so by offering a single simple API that hides interactions with more sophisticated components.
-
It gives more general functions, a context-specific interface.
-
It lowers the time complexity to learn how to use the subsystem effectively.
Structure
The façade pattern differs from the adapter pattern in that it simplifies a class structure while the adapter maintains the same structure.
Example
When ordering from a catalog, customers come across a facade. A customer care agent answers the phone when the consumer dials a single number. The customer service agent serves as a facade, acting as an intermediary between the order fulfilment, billing, and shipping departments.
The facade class in the next example is about restaurant food orders. To correctly execute a meal order, we rely on a precise sequence of method calls, each of which is reliant on the one before it, and so on. If the server does not write down the order and submits it to the kitchen, the dish will not be prepared. Because of the intricacy, the facade class exposes the orderFood task to the client to simplify it and avoids any misuse.
// from https://medium.com/@andreaspoyias/design-patterns-a-quick-guide-to-facade-pattern-16e3d2f1bfb6#include <iostream>using namespace std;class Waiter_Subsystem1{public:void writeOrder() { cout << " Waiter writes client's order\n";}void sendToKitchen(){ cout << " Send order to kitchen\n";}void serveCustomer(){ cout << " Yeeei customer is served!!!\n";}};class Kitchen_Subsystem2{public:void prepareFood(){ cout << " Cook food\n";}void callWaiter() { cout << " Call Waiter\n";}void washDishes() { cout << " Wash the dishes\n";}};class Order_Facade{private:Waiter_Subsystem1 waiter;Kitchen_Subsystem2 kitchen;public:void orderFood(){cout << "A series of interdependent calls on various subsystems:\n";waiter.writeOrder();waiter.sendToKitchen();kitchen.prepareFood();kitchen.callWaiter();waiter.serveCustomer();kitchen.washDishes();}};int main(int argc, char *argv[]){// Simple for the client// no need to know the order or the// dependencies among various subsystems.//Rather than calling all different functions individually thanks to facade I only have 1 function...Order_Facade facade;facade.orderFood();return 0;}
Explanation
-
Lines 6 to 12: We create a class named
Waiter_Subsystem1with three methods:writeOrder(),sendToKitchen(), andserveCustomer(). -
Lines 13 to 19: We create a class named
Kitchen_Subsystem2with three methods:prepareFood(),callWaiter(), andwashDishes(). -
Lines 21 to 37: We create a
Order_Facadeclass with one method,orderFood(). We also create objects ofWaiter_Subsystem1andKitchen_Subsystem2. -
Lines 39 to 48: We create the object of the
Order_Facadeclass and call theorderFood()function, which shows the output on the console accordingly.
Free Resources