Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

facade
design pattern
structural pattern

What is the facade design pattern?

Osamah Ahmad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

The facade design pattern 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;
}
The facade design pattern implementation

Explanation

  • Lines 6 to 12: We create a class named Waiter_Subsystem1 with three methods: writeOrder(), sendToKitchen(), and serveCustomer().

  • Lines 13 to 19: We create a class named Kitchen_Subsystem2 with three methods: prepareFood(), callWaiter(), and washDishes().

  • Lines 21 to 37: We create a Order_Facade class with one method, orderFood(). We also create objects of Waiter_Subsystem1 and Kitchen_Subsystem2.

  • Lines 39 to 48: We create the object of the Order_Facade class and call the orderFood() function, which shows the output on the console accordingly.

RELATED TAGS

facade
design pattern
structural pattern

CONTRIBUTOR

Osamah Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring