设计模式十一(桥接模式,python 语言实现)

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

假期旅游 从目的地角度可以分为 上海和大连,从方式角度可以分为跟团和独体

桥接模式把这两种分类连接起来可以进行选择。

 

源代码
# -*- coding: utf-8 -*-
#######################################################
# 
# tour.py
# Python implementation of the Class DaLian
# Generated by Enterprise Architect
# Created on:      11-十二月-2012 16:53:52
# 
#######################################################


from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future_builtins import *
    

class TravelForm(object):
    """This class defines the interface for implementation classes.
    """
    def __init__(self, form="stay at home"):
        self.form=form
        pass

    def GetForm(self):
        return self.form
        pass
    pass

class Group(TravelForm):
    """This class implements the Implementor interface and defines its concrete
    implementation.
    """
    def __init__(self, form="by group"):
        super(Group,self).__init__(form)        
        pass
    pass


class Independent(TravelForm):
    """This class implements the Implementor interface and defines its concrete
    implementation.
    """
    def __init__(self, form="by myself"):
        super(Independent,self).__init__(form)
        pass

class Destination(object):
    """This class (a) defines the abstraction's interface, and (b) maintains a
    reference to an object of type Implementor.
    """
    m_TravelForm= TravelForm()

    def __init__(self, info):
        self.info=info
        pass

    def GetInfo(self):
        # imp->Operation();
        return print(self.info + " " +self.form.GetForm())
        pass

    def SetForm(self, form):
        self.form=form
        pass


class DaLian(Destination):
    """This class extends the interface defined by Abstraction.
    """
    def __init__(self, info="Go to DaLian "):
        super(DaLian,self).__init__(info)
        pass


class ShangHai(Destination):
    """This class extends the interface defined by Abstraction.
    """
    def __init__(self, info="Go to ShangHai"):
        super(ShangHai,self).__init__(info)
        pass
#客户端
if(__name__=="__main__"):
    
    destination=ShangHai()
    destination.SetForm(Group())
    destination.GetInfo()
    
    
    destination=DaLian()
    destination.SetForm(Independent())
    destination.GetInfo()    


 

#运行结果

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值