设计模式十一(桥接模式,python 语言实现)
来源:互联网 发布:小额贷款全部软件 编辑:程序博客网 时间:2024/05/22 08:24
基本原理请参考相关书籍,这里直接给实例
假期旅游 从目的地角度可以分为 上海和大连,从方式角度可以分为跟团和独体
桥接模式把这两种分类连接起来可以进行选择。
源代码# -*- 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 divisionfrom __future__ import print_functionfrom __future__ import unicode_literalsfrom 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 passclass 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 passclass Independent(TravelForm): """This class implements the Implementor interface and defines its concrete implementation. """ def __init__(self, form="by myself"): super(Independent,self).__init__(form) passclass 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 passclass DaLian(Destination): """This class extends the interface defined by Abstraction. """ def __init__(self, info="Go to DaLian "): super(DaLian,self).__init__(info) passclass 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()
#运行结果
- 设计模式十一(桥接模式,python 语言实现)
- 设计模式(十一)桥接模式
- 设计模式八(原型模式,python语言实现)
- 设计模式九(单例模式,python语言实现)
- 设计模式十(适配器模式,python语言实现)
- 设计模式十二(组合模式,python语言实现)
- 设计模式十三(外观模式,python语言实现)
- 设计模式十五(命令模式,python语言实现)
- 设计模式十七(状态模式,python语言实现)
- 设计模式十八(观察者模式,python语言实现)
- 设计模式十九(中介者模式,python语言实现)
- 设计模式 (十一)桥模式(birdge)
- 设计模式 (十一)桥模式(birdge)
- 设计模式 (十一)桥模式(birdge)
- Python设计模式(十一)【原型模式】
- Python设计模式(二十一)【备忘录模式】
- 设计模式实现(二十一)---中介者模式(Mediator)
- 设计模式之桥接模式Python实现
- 使用wine 在ubuntu中安装软件,卸载后,如何去掉链接
- linux shell 字符串操作(长度,查找,替换)详解
- VS 2010 std::list.sort函数实现的非递归merge sort
- [转载]C++Assert()函数
- hibernate hql like 查询有特殊字符
- 设计模式十一(桥接模式,python 语言实现)
- 深入浅出单实例Singleton设计模式
- 系列一版本管理软件简介
- 第十五周上机任务-项目4
- gcc前端的基本结构
- 如何用PowerDesigner逆向工程导出ORACLE数据库表
- C++内联函数
- 百度面试
- linux逻辑运算符,逻辑表达式详解