外观模式

模式说明

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

模式结构图

程序示例

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

代码:

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

 

posted @ 2014-08-25 14:52  棉花年度  阅读(175)  评论(0编辑  收藏  举报