zoukankan      html  css  js  c++  java
  • 外观模式

    模式说明

    所谓外观模式就是提供一个统一的接口,用来访问子系统中的一群接口。

    模式结构图

    程序示例

    说明:灯光、荧屏、空调、电视一键开启、关闭

    代码:

    class Light(object):
        def on(self):
            print 'light turn on'
        def off(self):
            print 'light turn off'
    
    class Screen(object):
        def on(self):
            print 'screen turn on'
        def off(self):
            print 'screen turn off'
    
    class AirConditioner(object):
        def on(self):
            print 'AirConditioner turn on'
        def off(self):
            print 'AirConditioner turn off'
    
    class TV(object):
        def on(self):
            print 'TV turn on'
        def off(self):
            print 'TV turn off'
    
    class Facade(object):
        """description of class"""
        light=Light()
        screen=Screen()
        airconditioner = AirConditioner()
        tv=TV()
        def on(self):
            print 'one key all on'
            self.light.on()
            self.screen.on()
            self.airconditioner.on()
            self.tv.on()
            
        def off(self):
            print 'one key all off'
            self.light.off()
            self.screen.off()
            self.airconditioner.off()
            self.tv.off()
    
    if __name__=='__main__':
        facade=Facade()
        facade.on()
        facade.off()

    运行结果:

    参考来源:

    http://www.cnblogs.com/chenssy/p/3679190.html

    http://www.cnblogs.com/wuyuegb2312/archive/2013/04/09/3008320.html

    http://www.cnblogs.com/Terrylee/archive/2006/07/17/334911.html

  • 相关阅读:
    观察者模式
    简单工厂
    一个数组先按值排序,如果它的值有相同,就再按键排序(转)
    Python 一些好玩的函数
    python 一些基础知识
    python3 写CSV文件多一个空行的解决办法
    pandas学习笔记
    pycharm2017.1破解方法
    python的Debug调试
    python中字典的陷阱
  • 原文地址:https://www.cnblogs.com/cotton/p/3934963.html
Copyright © 2011-2022 走看看