Design Patterns in Python — Facade

Hardik Patel
3 min readMar 29, 2019

--

Facade pattern allows to hide complexity of system and provide simple interface to client of the service.

Before going through programming example, let’s understand a real-life example, Shopkeeper. In shopkeeper, many items are available but custom doesn’t know the exact location of the item in the store, so we ask shopkeeper with list of items and shopkeeper provides all the items which customer wants to buy as shopkeeper know where items are located. Shopkeeper is facade of the shop.

Another easy example is facebook-sdk library to user facebook APIs. It provides very easy and simple methods to access all the REST APIs in pythonic way. So, this library is a facade.

In short, Facade provides interface which can be simple, easy and beautiful to access for client, even though the system may have messy and very much complex subsystems.

There are three points which are involved to understand this patter.

  • Facade class — This class is for implementing interface which will be used by client class. This class will use services implemented in system.
  • System class — Multiple system classes might be there in the system and each system class is for specific purpose.
  • Client class — Client class is using facade class to access functionality of system. It could be hard to access system class directly, so client is using facade class instead.

keep following points in your mind before using this pattern to solve your problem in your project or product.

  • Multiple subsystems should not be heavily dependent on one another.
  • System is very much complex and hard to access for any client user.
  • You might want to provide interfaces for all the functionalities you have.

Let’s understand in programming style with an example of Preparing a food dish.

Preparing food dish includes three processes like Cut the vegetables, Boil Vegetables, Fry vegetables. All these three processes are like three sub system classes. So, let’s implement these sub system classes first.

class Cutter(object):
def cutVegetables(self):
print("All vegetables are cut")



class Boiler(object):
def boilVegetables(self):
print("All vegetables are boiled")

class Frier(object):
def fry(self):
print("All vegetables is mixed and fried.")

These above classes are not supposed to be available for client for some reason, so we have implemented facade class for that called Cook.

class Cook(object):
def prepareDish(self):
self.cutter = Cutter()
self.cutter.cutVegetables()


self.boiler = Boiler()
self.boiler.boilVegetables()


self.frier = Frier()
self.frier.fry()

In this class, client need to initiate this class and call prepareDish method to prepare dish and no need to know how all internal processes works. So here Cook is facade for client.

Full implementation of the example —

class Cook(object):
def prepareDish(self):
self.cutter = Cutter()
self.cutter.cutVegetables()

self.boiler = Boiler()
self.boiler.boilVegetables()

self.frier = Frier()
self.frier.fry()


class Cutter(object):
def cutVegetables(self):
print("All vegetables are cut")


class Boiler(object):
def boilVegetables(self):
print("All vegetables are boiled")


class Frier(object):
def fry(self):
print("All vegetables is mixed and fried.")


if __name__ == "__main__":
cook = Cook()
cook.prepareDish()

Benefits

  • Facade class collects all system classes, methods and provide required meaningful APIs so this way for client there will be minimum call. If any changes need to be done in sub systems, there will not be any change for facade class and that way it will be loosely coupled.
  • It makes easier to use and maintain bigger structural process.

Point to be noted

  • Mediator pattern is very similar to Facade pattern as it also abstracts the functionalities from sub systems similar to Facade. But one notable difference is that sub systems are aware of Mediator class whereas in Facade pattern sub systems don’t know about Facade class at all.

Thank you for reading and keep learning.

--

--

Responses (1)