设计模式十三(外观模式,python语言实现)
2012-12-12 11:22
621 查看
基本原理请参考相关书籍,这里直接给实例
基本说明:外观模式不改变原有系统的结构,通过组建外观类提供对外交互的复杂工作。
今天上班老板交代两项工作:1.安排人扫雪。2.安派人准备接待参观考察团
公司原有的机构共三个部门A,B,C
为了完成老板安排的任务,我只好充当外观类,安排A,C部门扫雪(雪很大,需要两个部门),安排B部门接待参观(B部门姐姐,妹妹多能营造氛围)
运行结果:
基本说明:外观模式不改变原有系统的结构,通过组建外观类提供对外交互的复杂工作。
今天上班老板交代两项工作:1.安排人扫雪。2.安派人准备接待参观考察团
公司原有的机构共三个部门A,B,C
为了完成老板安排的任务,我只好充当外观类,安排A,C部门扫雪(雪很大,需要两个部门),安排B部门接待参观(B部门姐姐,妹妹多能营造氛围)
# -*- coding: utf-8 -*- ####################################################### # # Facade.py # Python implementation of the Class client # Generated by Enterprise Architect # Created on: 12-十二月-2012 10:52:19 # Original author: zjm # ####################################################### from __future__ import division from __future__ import print_function from __future__ import unicode_literals from future_builtins import * class Department(object): """This class (a) implements subsystem functionality, (b) handles work assigned by the Facade object, and (c) keeps no reference to the facade. """ def __init__(self, name="None"): self.name=name pass def Operation(self): print(self.name+ " "+"does this work") pass class DepartmentA(Department): """This class (a) implements subsystem functionality, (b) handles work assigned by the Facade object, and (c) keeps no reference to the facade. """ def __init__(self, name="DepartmentA"): super(DepartmentA,self).__init__(name) pass class DepartmentB(Department): """This class (a) implements subsystem functionality, (b) handles work assigned by the Facade object, and (c) keeps no reference to the facade. """ def __init__(self, name="DepartmentB"): super(DepartmentB,self).__init__(name) pass class DepartmentC(Department): """This class (a) implements subsystem functionality, (b) handles work assigned by the Facade object, and (c) keeps no reference to the facade. """ def __init__(self, name="DepartmentC"): super(DepartmentC,self).__init__(name) pass class Facade(object): """This class (a) knows which subsystem classes are responsible for a request, and (b) delegates client requests to appropriate subsystem objects. """ m_DepartmentA= DepartmentA() m_DepartmentB= DepartmentB() m_DepartmentC= DepartmentC() def __init__(self): self.m_DepartmentA= DepartmentA() self.m_DepartmentB= DepartmentB() self.m_DepartmentC= DepartmentC() def CleanSnow(self): print("CleanSnow:") self.m_DepartmentA.Operation() self.m_DepartmentC.Operation() pass def ReceiveVisit(self): print("ReceiveVisit:") self.m_DepartmentB.Operation() pass #客户端(老板) if(__name__=="__main__"): class client: m_Facade= Facade() m_Facade.CleanSnow() m_Facade.ReceiveVisit()
运行结果:
相关文章推荐
- 设计模式十(适配器模式,python语言实现)
- 设计模式十七(状态模式,python语言实现)
- 设计模式十四(享元模式flyweight,python语言实现)
- 设计模式九(单例模式,python语言实现)
- 设计模式八(原型模式,python语言实现)
- 设计模式十二(组合模式,python语言实现)
- 设计模式十八(观察者模式,python语言实现)
- 设计模式十一(桥接模式,python 语言实现)
- 设计模式十九(中介者模式,python语言实现)
- 设计模式之单例模式——Python实现
- python 设计模式实现
- 设计模式之单例模式java语言的实现
- 基于python智慧树刷课脚本在C#语言下的设计与实现
- [python实现设计模式]-4.观察者模式-吃食啦!
- 设计模式C++实现(7)——外观模式、组合模式
- 常见设计模式的解析和实现(C++)之十三-FlyWeight模式
- 设计模式之外观模式 c++实现
- 设计模式的解析和实现(C++)之十三-FlyWeight模式
- java语言实现创建型设计模式—抽象工厂模式
- 23种设计模式之python实现--总结