定义: 外观模式
内容: 为子系统中的 一组接口提供一个一致的界面, 外观模式定义了一个 高层接口,这个接口使得这一子系统更加容易使用;
角色
外观 facade;
子系统类; subsystem classes ;
优点:
- 减少系统相互依赖;
- 提高了灵活性;
- 提高安全型;
代码实例
底层代码:
class CPU:
def run(self):
print(" cpu start running ")
def stop(self):
print( ' cpu stop running ')
class Disk:
def run(self):
print(" hard disk start running ")
def stop(self):
print(' hard disk stop runing ')
class Memory:
def run(self):
print(' memory power on')
def stop(self):
print( " memory power off ")
class Computer: # Facade
def __init__(self):
self.cpu = CPU()
self.disk = Disk()
self.memory = Memory()
def run(self):
self.cpu.run()
self.disk.run()
self.memory.run()
def stop(self):
self.cpu.stop()
self.disk.stop()
self.memory.stop()
高层代码
#Client
computer = Computer()
computer.run()
computer.stop()