Facade Design Pattern

Qasimali
2 min readOct 24, 2022

--

The Beauty of Design pattern Series (12 of 20)

A Facade is to create an interface (class) to simplify other libraries, a framework, or any other complex classes. The facade class hides the framework’s complexity behind a simple interface. Basically, the Facade Pattern is about making complicated things simple.

It’s a trade-off between functionality and simplicity, meaning the more simply the new facade class as an interface, the less functionality it has. This facade class will be useful for something that we do repeatly. For example, if we order a book from amazon.com, with the usual process, we have to fill in the address and method of payment, but with the new system as facade class called Buy Now with 1-Click, we don’t have to fill in the address and method of payment since the system will take the information from previous order or as default address and method of payment.

The following is the code’s story

For the first order, we have to fill in three pieces of information (food that we want to order, destination address, and method of payment). For the second order, since the apps already save our data, they only need the food that we want to order. In fact, if we want to repeat an order from the first order, we just need to press click on repeat order and done.

The following source code below has three classes. class Order to manage order data and class FoodOrder to manage input and output data for orders. For the first order, we use the FoodOrder class since the system still doesn’t have an address and method of payment. For the second order, we use the class FacadeFoodOrder with information about the food to order and the previous order. If you compare the number of lines when we order for first_order and second_order, it is reduced by 50%, from 6 lines to 3 lines. This is what the facade design pattern function does. We create new classes to simplify existing classes or subsystems.

This github repository contains all of the source code for this design pattern series.

--

--