DuFacade.py
import os import re import threading # 外观模式 Facade Pattern class _IgnitionSystem(object): @staticmethod def produce_spark(): return True class _Engine(object): def __init__(self): self.revs_per_minute = 0 def turnon(self): self.revs_per_minute = 2000 def turnoff(self): self.revs_per_minute = 0 class _FuelTank(object): def __init__(self, level=30): self._level = level @property def level(self): return self._level @level.setter def level(self, level): self._level = level class _DashBoardLight(object): def __init__(self, is_on=False): self._is_on = is_on def __str__(self): return self.__class__.__name__ @property def is_on(self): return self._is_on @is_on.setter def is_on(self, status): self._is_on = status def status_check(self): if self._is_on: print("{}: 开启".format(str(self))) else: print("{}: 关闭".format(str(self))) class _HandBrakeLight(_DashBoardLight): pass class _FogLampLight(_DashBoardLight): pass class _Dashboard(object): def __init__(self): self.lights = {"手刹": _HandBrakeLight(), "雾": _FogLampLight()} def show(self): for light in self.lights.values(): light.status_check() # Facade 列要对齐,否则方法(函数)调用不出来 Geovin Du,geovindu,涂聚文注,格式很关键 class Car(object): def __init__(self): self.ignition_system = _IgnitionSystem() self.engine = _Engine() self.fuel_tank = _FuelTank() self.dashboard = _Dashboard() @property def km_per_litre(self): return 17.0 def consume_fuel(self, km): litres = min(self.fuel_tank.level, km / self.km_per_litre) self.fuel_tank.level -= litres def start(self): print("\n开始启动...") self.dashboard.show() if self.ignition_system.produce_spark(): self.engine.turnon() else: print("不能开始。点火系统故障") def has_enough_fuel(self, km, km_per_litre): litres_needed = km / km_per_litre if self.fuel_tank.level > litres_needed: return True else: return False def drive(self, km=100): print("\n") if self.engine.revs_per_minute > 0: while self.has_enough_fuel(km, self.km_per_litre): self.consume_fuel(km) print("geovindu开车 {}km".format(km)) print("剩余的燃料 {:.2f}l ".format(self.fuel_tank.level)) else: print("不能开车。引擎已关闭!") def park(self): print("\n停车...") self.dashboard.lights["手刹"].is_on = True self.dashboard.show() self.engine.turnoff() def switch_fog_lights(self, status): print("\n切换 {} 雾灯...".format(status)) boolean = True if status == "ON" else False self.dashboard.lights["雾"].is_on = boolean self.dashboard.show() def fill_up_tank(self): print("\n油箱已满!") self.fuel_tank.level = 100 # the main function is the Client def main(): car = Car() car.start() car.drive() car.switch_fog_lights("ON") car.switch_fog_lights("OFF") car.park() car.fill_up_tank() car.drive() car.start() car.drive()
调用:
main.py
# 外观模式 Facade Pattern # DuFacade.main() car = DuFacade.Car() car.start() car.drive() car.switch_fog_lights("ON") car.switch_fog_lights("OFF") car.park() car.fill_up_tank() car.drive() car.start() car.drive()
输出:
开始启动... _HandBrakeLight: 关闭 _FogLampLight: 关闭 开车 100km 剩余的燃料 24.12l 开车 100km 剩余的燃料 18.24l 开车 100km 剩余的燃料 12.35l 开车 100km 剩余的燃料 6.47l 开车 100km 剩余的燃料 0.59l 切换 ON 雾灯... _HandBrakeLight: 关闭 _FogLampLight: 开启 切换 OFF 雾灯... _HandBrakeLight: 关闭 _FogLampLight: 关闭 停车... _HandBrakeLight: 开启 _FogLampLight: 关闭 油箱已满! 不能开车。引擎已关闭! 开始启动... _HandBrakeLight: 开启 _FogLampLight: 关闭 开车 100km 剩余的燃料 94.12l 开车 100km 剩余的燃料 88.24l 开车 100km 剩余的燃料 82.35l 开车 100km 剩余的燃料 76.47l 开车 100km 剩余的燃料 70.59l 开车 100km 剩余的燃料 64.71l 开车 100km 剩余的燃料 58.82l 开车 100km 剩余的燃料 52.94l 开车 100km 剩余的燃料 47.06l 开车 100km 剩余的燃料 41.18l 开车 100km 剩余的燃料 35.29l 开车 100km 剩余的燃料 29.41l 开车 100km 剩余的燃料 23.53l 开车 100km 剩余的燃料 17.65l 开车 100km 剩余的燃料 11.76l 开车 100km 剩余的燃料 5.88l 开车 100km 剩余的燃料 0.00l Process finished with exit code 0