设计模式(Python语言)----外观模式
  TEZNKK3IfmPf 2023年11月14日 23 0

外观模式内容

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

外观模式中的角色

  • 外观(facade)
  • 子系统类(subsystem classes)

外观模式的优点

  • 减少了系统的相互依赖
  • 提高了灵活性
  • 提高了安全性

外观模式实例

代码如下

from abc import ABCMeta,abstractmethod

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(object):
    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()

if __name__=="__main__":
    computer=Computer()
    computer.run()
    computer.stop()

执行结果如下:

CPU开始运行...
硬盘开始工作...
内存通电
CPU停止运行...
硬盘停止工作...
内存断电

推荐阅读

设计模式(Python语言)----面向对象设计SOLID原则

设计模式(Python语言)----设计模式分类

设计模式(Python语言)----简单工厂模式

设计模式(Python语言)----工厂方法模式

设计模式(Python语言)----抽象工厂模式

设计模式(Python语言)----建造者模式

设计模式(Python语言)----单例模式

设计模式(Python语言)----适配器模式

设计模式(Python语言)----桥模式

设计模式(Python语言)----组合模式

设计模式(Python语言)----外观模式

设计模式(Python语言)----代理模式

设计模式(Python语言)----责任链模式

设计模式(Python语言)----观察者模式

设计模式(Python语言)----策略模式

设计模式(Python语言)----模板方法模式

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   36   0   0 python开发语言
  TEZNKK3IfmPf   2024年05月31日   27   0   0 python
  TEZNKK3IfmPf   2024年05月31日   35   0   0 excelpython
  TEZNKK3IfmPf   2024年05月31日   28   0   0 python
TEZNKK3IfmPf