The Facade design pattern belongs to the structural design patterns and provides a unified interface to a set of interfaces. It is commonly used in apps written in OOP languages and it intends to provide a convenient interface to the client, thus the latter avoid dealing with complex parts (subsystems). Keep in mind although that, the client should still be able to have access to the functionality of the complex parts.
Example #1
Let us assume we want to implement a simple order process. In this order, we will include an inventory check and the payment handler.
The two simplified classes could look something like this:
class Inventory: def check_inventory(self): print("Inventory Checked") class Payment: def proccess_payment(self): print("Payment completed")
This is very simple and any client could use them easily but it is always better as mentioned earlier to provide a unified interface by using the Facade pattern.
class OrderFacade: def __init__(self): self.inventory = Inventory() self.payment = Payment() def procces_order(self): self.inventory.check_inventory() self.payment.proccess_payment() print("Order completed")
In the OrderFacade class, we initialize each subsystem we want to include in the unified interface and we use what it is needed from each one to provide simplified access to the client.
The client can be another class or even a main method.
def main(): order = OrderFacade() order.procces_order() main()
Inventory Checked
Payment completed
Order completed
Example #2
Let us assume now that we want to create an application similar to Booking.com where a user would be able to search for his/her traveling flights, hotels, and transfers.
Those can be described like this for our example:
class Hotel: def get_available_hotels(self): print("Hotels available at the moment: Hitlon, Best Western") class Flight: def get_flights(self): print("Flights every day at 06:00 and 20:00") class Transfer: def get_transfers(self): print("Not Available")
Let’s implement now the Facade class which will allow any client to perform those searches simultaneously.
class TravelFacade: def __init__(self): self.hotel = Hotel() self.flight = Flight() self.trasnfer = Transfer() def check_availability(self): self.hotel.get_available_hotels() self.flight.get_flights() self.trasnfer.get_transfers() print("Search Completed")
As earlier, we will create the main method as the client.
def main(): travel = TravelFacade() travel.check_availability() main()
Hotels available at the moment: Hitlon, Best Western
Flights every day at 06:00 and 20:00
Not Available
Search Completed
Overall except that we can provide simpler interfaces to the clients the usage of the Facade pattern allows us to decouple the client from all the subsystems thus we are able to make changes to any subsystem without affecting the clients.
Last, as a note, keep in mind that what is important for design patters is not to learn them, this can happen while studying, practicing and gaining more experience, but be able to recognize when to use which one.