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

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

 

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

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

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值