外观模式
概述
- 为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口是的这一子系统更容易使用
对象
- 外观(facade)
- 子系统类(subsystem classes)
例子
class CPU:
def run(self):
print("CPU run...")
def stop(self):
print("CPU stop...")
class Disk:
def run(self):
print("Disk run...")
def stop(self):
print("Disk stop...")
class Memory:
def run(self):
print("Memory run...")
def stop(self):
print("Memory stop...")
class Computer:
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()
computer = Computer()
computer.run()
computer.stop()
优点