Skip to content

Instantly share code, notes, and snippets.

@simongarisch
Created October 16, 2019 22:01
Show Gist options
  • Save simongarisch/c616691e9b313fd49d304eb4cdc341c0 to your computer and use it in GitHub Desktop.
Save simongarisch/c616691e9b313fd49d304eb4cdc341c0 to your computer and use it in GitHub Desktop.
Python structural patterns - facade
"""
Provide a simpler interface to a more complex problem.
Think pressing a button to turn a computer on.
from https://github.com/faif/python-patterns/blob/master/patterns/structural/facade.py
"""
class CPU(object):
def freeze(self):
print("Freezing processor.")
def jump(self, position):
print("Jumping to:", position)
def execute(self):
print("Executing.")
class Memory(object):
def load(self, position, data):
print("Loading from {0} data: '{1}'.".format(position, data))
class SolidStateDrive(object):
def read(self, lba, size):
return "Some data from sector {0} with size {1}".format(lba, size)
class ComputerFacade(object):
def __init__(self):
self.cpu = CPU()
self.memory = Memory()
self.ssd = SolidStateDrive()
def start(self):
self.cpu.freeze()
self.memory.load("0x00", self.ssd.read("100", "1024"))
self.cpu.jump("0x00")
self.cpu.execute()
computer_facade = ComputerFacade()
computer_facade.start()
"""
Freezing processor.
Loading from 0x00 data: 'Some data from sector 100 with size 1024'.
Jumping to: 0x00
Executing.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment