首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

设计形式十三(外观模式,python语言实现)

2012-12-14 
设计模式十三(外观模式,python语言实现)基本原理请参考相关书籍,这里直接给实例 基本说明:外观模式不改变

设计模式十三(外观模式,python语言实现)

基本原理请参考相关书籍,这里直接给实例

 

基本说明:外观模式不改变原有系统的结构,通过组建外观类提供对外交互的复杂工作。

                  今天上班老板交代两项工作:1.安排人扫雪。2.安派人准备接待参观考察团

                   公司原有的机构共三个部门A,B,C

                  为了完成老板安排的任务,我只好充当外观类,安排A,C部门扫雪(雪很大,需要两个部门),安排B部门接待参观(B部门姐姐,妹妹多能营造氛围)

 

 

设计形式十三(外观模式,python语言实现)

 

# -*- 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 divisionfrom __future__ import print_functionfrom __future__ import unicode_literalsfrom 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")        passclass 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)        passclass 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)        passclass 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)        passclass 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语言实现)

热点排行