【Python设计模式】外观模式

花间影清欢课程 2024-03-27 11:09:42
外观模式内容: 为子系统中的一组接口提供一个一致的界面, 外观模式定义了一个高层接口, 这个接口使得这一子系统更加容易使用。角色: - 外观(facade) - 子系统类(subsystemes)优点: - 减少系统相互依赖 - 提高了灵活性 - 提高了安全性

示例

class CPU: def run(self): print('CPU开始运行') def stop(self): print('CPU停止运行')class Disk: def run(self): print('硬盘开始工作') def stop(self): print('硬盘停止工作')class Memory: def run(self): print('内存通电') def stop(self): print('内存停电')class Computer: def __init__(self): self.cpu = CPU() self.disk = Disk() self.memory = Memory() def run(self): print('-----------计算机开机-----------') self.cpu.run() self.disk.run() self.memory.run() def stop(self): print('-----------计算机关机-----------') self.cpu.stop() self.disk.stop() self.memory.stop()computer = Computer()computer.run()computer.stop()

运行结果

0 阅读:0