内容: 为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一系统更加容易使用
角色
- 外观 (facade)
- 子系统类(subsystem classes)
#子系统
class CPU:
def run(self):
print("CPU开始运行")
def stop(self):
print("CPU停止运行")
#子系统
class Disk:
def run(self):
print("键盘停止工作")
def stop(self):
print("硬盘停止工作")
#子系统
class Memoney:
def run(self):
print("内存通电")
def stop(self):
print("内存断电")
#更高级的系统调用子系统
class Computer:
def __init__(self):
self.cpu = CPU()
self.disk = Disk()
self.memonry = Memoney()
def run(self):
self.cpu.run()
self.disk.run()
self.memonry.run()
def stop(self):
self.cpu.stop()
self.disk.stop()
self.memonry.stop()
computer = Computer()
computer.run()
computer.stop()
优点:
- 减少系统互相依赖
- 提高了灵活性
- 提高了安全性