Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system. This type of design pattern comes under structural pattern as this pattern adds an interface to existing system to hide its complexities.
class Compiler:
def __init__(self, path):
self.path = path
def compile(self):
tokenizer = Tokenizer(self.path).tokenize()
parser = Parser(tokenizer).parse()
builder = IRBuilder(parser).build()
optimizer = Optimizer(builder).optimize()
codegenerator = CodeGenerator(optimizer).generate()
linker = Linker(codegenerator).link()
return linker
class Tokenizer:
def __init__(self, path):
self.path = path
def tokenize(self):
print(f"tokenizing {self.path}")
return self
def __str__(self):
return "Tokenizer"
class Parser:
def __init__(self, tokenizer):
self.tokenizer = tokenizer
def parse(self):
print("parsinging", end=" ")
print(self.tokenizer)
return self
def __str__(self):
return "Parser"
class IRBuilder:
def __init__(self, parser):
self.parser = parser
def build(self):
print("IRbuilding", end=" ")
print(self.parser)
return self
def __str__(self):
return "IRBuilder"
class Optimizer:
def __init__(self, builder):
self.builder = builder
def optimize(self):
print("optimizing", end=" ")
print(self.builder)
return self
def __str__(self):
return "Optimizer"
class CodeGenerator:
def __init__(self, optimizer):
self.optimizer = optimizer
def generate(self):
print("generating code", end=" ")
print(self.optimizer)
return self
def __str__(self):
return "CodeGenerator"
class Linker:
def __init__(self, generator):
self.codegenerator = generator
def link(self):
print("linking",end=" ")
print(self.codegenerator)
return self
def __str__(self):
return "Linker"
compiler = Compiler("sample.py")
compiler.compile()
class Washing:
'''Subsystem # 1'''
def wash(self):
print("Washing...")
class Rinsing:
'''Subsystem # 2'''
def rinse(self):
print("Rinsing...")
class Spinning:
'''Subsystem # 3'''
def spin(self):
print("Spinning...")
class WashingMachine:
'''Facade'''
def __init__(self):
self.washing = Washing()
self.rinsing = Rinsing()
self.spinning = Spinning()
def startWashing(self):
self.washing.wash()
self.rinsing.rinse()
self.spinning.spin()