外观模式

内容:为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,
     这个接口使得这一子系统更加容易使用

角色:
    外观(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 Memory:
    def run(self):
        print("内存通电")

    def stop(self):
        print("内存断电")

# ---------------------------------------
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()

computer = Computer()
computer.run()
computer.stop()

posted on 2023-02-01 14:07  夜黎i  阅读(14)  评论(0)    收藏  举报

导航