The facade method is a type of structural design pattern that allows the use of a simplified interface to a framework, a library or any other complex set of classes. This method can use any subsystem because it defines a higher-level interface, and it knows which subsystem is responsible for a request.
The implementation of the facade method works using the following guidelines:
- Scrutinize first to see if it’s possible to provide a more straightforward interface than what an existing subsystem already provides.
- Define and Implement this interface in a new facade class.
- Make all the client code communicate with the subsystem.
- If the facade class is too big, try extracting part of its behaviour to a new facade class.
Imagine we have a computerized pressing Iron which can press the clothes and then fold them but does all the task separately. We need a system that can automate the whole job without the disturbance.
To solve the pressing iron problem, we would like to use the Facade method. It helps to hide the complexities of the subsystems as follows. The following code is written in python using the facade method.
class Spreading: """ Subsystem #1 """ def spread(self): print("Spreading the cloth...\n") class Pressing: """ Subsystem #2 """ def press(self): print("Pressing the cloth........\n") class Folding: """ Subsystem #3 """ def fold(self): print("Folding the pressed cloth......\n") print("Thank you for pressing your cloth......") class PressingMachine: """ Facade Class """ def __init__(self): self.spreading = Spreading() self.pressing = Pressing() self.folding = Folding() def StartPressing(self): self.spreading.spread() self.pressing.press() self.folding.fold() """ main method """ if __name__ == '__main__': # Start the machine print("Put your cloth on the machine\n") pressingMachine = PressingMachine() pressingMachine.StartPressing()
ProBook:~/InfosecAddicts/$ python facade.py Put your cloth on the machine Spreading the cloth... Pressing the cloth........ Folding the pressed cloth......
Pros and Cons
- Loose Coupling
- Costly process
- Changes in Method