Facade

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Facade Design Pattern in Python

Facade Design Pattern in Python

Design Patterns Home

What is it?

The Facade Design Pattern is a Structural Design Pattern. The word facade means the outerlying interface of a complex system, made up of several subsystems. In order to hide or abstract the complexities of the subsystems, a facade class is used. For example, while booting a computer system, many composite systems are initialized, such as processing unit, display unit, memory, peripherals. The facade in this case is the Power button located on the CPU.

It is classified under Structural Design Patterns as it offers one of the best ways to organize class hierarchy.


Why the need for it: Problem Statement

In order to abstract the complexities of subsystems from the client, the Facade Pattern is employed.


Terminology

  • Facade: A class acting as an interface between the subsystems and client.

Pseudo Code

The example below is hardly a useful one, but it helps to demonstrate the pattern. You can easily scale it to suit your needs.

class ProcessingUnit:
'''Subsystem #1'''

class DisplayUnit:
'''Subsystem #2'''

class Memory:
'''Subsystem #3'''

class Computer:
'''Facade'''
def __init__(self):
'''create objects of subsystems and assign to instance variables'''

def bootUp(self):
'''a method of facade which invokes relevant methods of subsystems'''

# USING THE CODE
create object of the facade
invoke method of facade which invokes relevant methods of subsystems

How to implement it

class ProcessingUnit:
'''Subsystem #1'''
def process(self):
print("Processing...")

class DisplayUnit:
'''Subsystem #2'''
def display(self):
print("Displaying...")

class Memory:
'''Subsystem #3'''
def ioOperation(self):
print("Reading and writing to memory...")

class Computer:
'''Facade'''
def __init__(self):
self.processingUnit = ProcessingUnit()
self.displayUnit = DisplayUnit()
self.memory = Memory()

def bootUp(self):
self.processingUnit.process()
self.memory.ioOperation()
self.displayUnit.display()

computer = Computer()
computer.bootUp()

As you can see, there is nothing complicated about the Facade Pattern. It is designed to hide the intricacy of a complex system by providing the client with a class which acts as an interface.



See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply