您的位置:首页 > 编程语言 > Python开发

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

2012-12-12 11:22 621 查看
基本原理请参考相关书籍,这里直接给实例



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

今天上班老板交代两项工作: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()

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: