结构型模式(三) 外观模式

发布时间 2023-10-26 14:33:21作者: longfei2021

外观模式:是为了给子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得子系统更加容易使用。减少系统之间的耦合性,提高了灵活性和安全性

角色:外观类、子系统类

class Cpu:
    def start(self):
        print('cpu start')

    def stop(self):
        print('cpu stop')

class Memory:
    def start(self):
        print('memory start')

    def stop(self):
        print('memory stop')

class Disk:
    def start(self):
        print('disk start')

    def stop(self):
        print('disk stop')


class Computer:
    def __init__(self):
        self.cpu = Cpu()
        self.memory = Memory()
        self.disk = Disk()

    def start(self):
        self.cpu.start()
        self.memory.start()
        self.disk.start()

    def stop(self):
        self.cpu.stop()
        self.memory.stop()
        self.disk.stop()


computer = Computer()
computer.start()
computer.stop()

cpu start
memory start
disk start
cpu stop
memory stop
disk stop