facade_wiki.py
"""UML
--------------> ModuleB
| / |
Facade --> ModuleA |
| \ |
--------------> ModuleC
"""
# Module B
class Car:
def __init__(self):
self.__speed = 0
self.__distance = 0
@property
def speed(self) -> int:
return self.__speed
@speed.setter
def speed(self, speed: int):
self.__speed = speed
@property
def distance(self) -> int:
return self.__distance
def run(self, minutes: int):
self.__distance += self.__speed * minutes
# Module A
class Driver:
def __init__(self, car: Car):
self.__car = car
def push_pedal(self, speed: int):
self.__car.speed = speed
def drive(self, minutes: int):
self.__car.run(minutes)
# Facade
class DrivingSimulator:
"""Point
Facadeクラスはあくまでサブシステム内部に仕事を投げるだけで複雑な実装は持たない
"""
def simulate(self):
car = Car()
driver = Driver(car)
driver.push_pedal(700)
driver.drive(30)
driver.push_pedal(750)
driver.drive(20)
print(f'The travel distance is {car.distance}m')
# Client
if __name__ == '__main__':
driving_simulator = DrivingSimulator()
driving_simulator.simulate()