This is the first of actual posts in my series on design patterns in Python.
This will be about the Facade pattern which is a structural pattern.
The facade pattern is used to make one object with a simple interface represent a complicated system. The problem often occurs in programming where you have a series of interconnected classes where the functions must be called in a certain order or have complicated interdependencies.
This pattern is to give a standard interface to such a system, so you don’t have to rely on reading how you call the system in one of the files or look at example usage.
Here is a UML diagram representing the pattern.
UML for Facade pattern
UML is a very useful tool however I have not used a lot yet. It provides a (mostly) standardised way of representing system structure for object oriented programming. I intend to use a UML diagram to represent each pattern in this series, I will explain them so hopefully you don’t need to know UML to follow what’s going on.
This is a nice and simple UML diagram with the following elements; the facade box represents a class, the boxes in the subsystem box could be any object such as a class or a function and the lines between them indicate some relationship.
This shows that the pattern is just one class which groups together a lot of other objects and uses them in some way.
An Example Usage
Here is an implementation of this in python. For this example I am using a car. As this is a small example I will not create a whole complex sub-system, just a few classes.
Here are the three classes which make up the sub-system; Engine, StarterMotor and Battery.
# how much the motor is spinning in revs per minute
self.spin = 0
def start(self, spin):
if (spin > 2000):
self.spin = spin
# how much the starter motor is spinning in revs per minute
self.spin = 0
def start(self, charge):
# if there is enough power then spin fast
if (charge > 50):
self.spin = 2500
# % charged, starts flat
self.charge = 0
So now we need our facade object which will work as a common interface for the car.
# the facade object that deals with the battery, engine and starter motor.
self.battery = Battery()
self.starter = StarterMotor()
self.engine = Engine()
# use the battery to turn the starter motor
# use the starter motor to spin the engine
# if the engine is spinning the car is started
if (self.engine.spin > 0):
print("Engine Not Started.")
self.battery.charge = 100
This enables the user of Car to use the system as it was intended to be used. I include this at the bottom of the python module so that on running it makes a car, tries to start it, then jumps it to start it.
if (__name__ == "__main__"):
corsa = Car()
The output of this program is the following;
Engine Not Started.
That is a simple example of the facade design pattern. The code for this is on my GitHub here.
All of the code for this series can be found in this repository.