实例解析Python设计模式编程之桥接模式的运用


Posted in Python onMarch 02, 2016

我们先来看一个例子:

#encoding=utf-8 
# 
#by panda 
#桥接模式 
 
def printInfo(info): 
  print unicode(info, 'utf-8').encode('gbk') 
 
#抽象类:手机品牌 
class HandsetBrand(): 
  soft = None 
  def SetHandsetSoft(self, soft): 
    self.soft = soft 
   
  def Run(self): 
    pass 
   
#具体抽象类:手机品牌1 
class HandsetBrand1(HandsetBrand): 
  def Run(self): 
    printInfo('手机品牌1:') 
    self.soft.Run() 
 
#具体抽象类:手机品牌2 
class HandsetBrand2(HandsetBrand): 
  def Run(self): 
    printInfo('手机品牌2:') 
    self.soft.Run() 
 
   
#功能类:手机软件 
class HandsetSoft(): 
  def Run(self): 
    pass 
 
#具体功能类:游戏   
class HandsetGame(HandsetSoft): 
  def Run(self): 
    printInfo('运行手机游戏') 
     
#具体功能类:通讯录   
class HandsetAddressList(HandsetSoft): 
  def Run(self): 
    printInfo('运行手机通信录') 
 
def clientUI(): 
  h1 = HandsetBrand1() 
  h1.SetHandsetSoft(HandsetAddressList()) 
  h1.Run() 
  h1.SetHandsetSoft(HandsetGame()) 
  h1.Run() 
   
  h2 = HandsetBrand2() 
  h2.SetHandsetSoft(HandsetAddressList()) 
  h2.Run() 
  h2.SetHandsetSoft(HandsetGame()) 
  h2.Run()   
  return 
 
if __name__ == '__main__': 
  clientUI();

可以总结出类图是这样的: 

实例解析Python设计模式编程之桥接模式的运用

所以,桥接模式的概念在于将系统抽象部分与它的实现部分分离,使它们可以独立地变化。
由于目标系统存在多个角度的分类,每一种分类都会有多种变化,那么就可以把多角度分离出来,让它们独立变化,减少它们之间的耦合。

下面我们再来看一个实例:

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

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

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

类图:

实例解析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()

运行结果

实例解析Python设计模式编程之桥接模式的运用

Python 相关文章推荐
Python批量查询域名是否被注册过
Jun 21 Python
深入理解Django的自定义过滤器
Oct 17 Python
Python实现的单向循环链表功能示例
Nov 10 Python
Python在for循环中更改list值的方法【推荐】
Aug 17 Python
Python sorted函数详解(高级篇)
Sep 18 Python
IntelliJ IDEA安装运行python插件方法
Dec 10 Python
Python列表list排列组合操作示例
Dec 18 Python
Python使用scrapy爬取阳光热线问政平台过程解析
Aug 14 Python
Django权限设置及验证方式
May 13 Python
python判断正负数方式
Jun 03 Python
Python爬虫小例子——爬取51job发布的工作职位
Jul 10 Python
深入浅析python3 依赖倒置原则(示例代码)
Jul 09 Python
Python随机生成带特殊字符的密码
Mar 02 #Python
Python设计模式编程中Adapter适配器模式的使用实例
Mar 02 #Python
Python打造出适合自己的定制化Eclipse IDE
Mar 02 #Python
设计模式中的原型模式在Python程序中的应用示例
Mar 02 #Python
深入解析Python设计模式编程中建造者模式的使用
Mar 02 #Python
举例讲解Python设计模式编程中对抽象工厂模式的运用
Mar 02 #Python
实例讲解Python设计模式编程之工厂方法模式的使用
Mar 02 #Python
You might like
解析php二分法查找数组是否包含某一元素
2013/05/23 PHP
php获取文件后缀的9种方法
2016/03/22 PHP
PHP微信支付实例解析
2016/07/22 PHP
php利用ffmpeg提取视频中音频与视频画面的方法详解
2017/06/07 PHP
PHP的RSA加密解密方法以及开发接口使用
2018/02/11 PHP
基于jQuery插件实现点击小图显示大图效果
2016/05/11 Javascript
js和jquery中获取非行间样式
2017/05/05 jQuery
解决html input验证只能输入数字,不能输入其他的问题
2017/07/21 Javascript
Vue-Router进阶之滚动行为详解
2017/09/13 Javascript
jQuery 改变P标签文本值方法
2018/02/24 jQuery
vue项目中公用footer组件底部位置的适配问题
2018/05/10 Javascript
前端vue-cli项目中使用img图片和background背景图的几种方法
2019/11/13 Javascript
vue表单数据交互提交演示教程
2019/11/13 Javascript
小程序实现长按保存图片的方法
2019/12/31 Javascript
python数据结构之图深度优先和广度优先实例详解
2015/07/08 Python
利用Python如何生成随机密码
2016/04/20 Python
Python连接SQLServer2000的方法详解
2017/04/19 Python
Python变量赋值的秘密分享
2018/04/03 Python
Python 实现选择排序的算法步骤
2018/04/22 Python
搞清楚 Python traceback的具体使用方法
2019/05/13 Python
Python之关于类变量的两种赋值区别详解
2020/03/12 Python
Python 字节流,字符串,十六进制相互转换实例(binascii,bytes)
2020/05/11 Python
什么是Python中的匿名函数
2020/06/02 Python
如何用python开发Zeroc Ice应用
2021/01/29 Python
用HTML5.0制作网页的教程
2010/05/30 HTML / CSS
HTML5拖拉上传文件的简单实例
2017/01/11 HTML / CSS
纽约州一群才华横溢的金匠制作而成:Hearth Jewelry
2019/03/22 全球购物
李维斯牛仔裤荷兰官方网站:Levi’s NL
2020/08/23 全球购物
美国最大最全的亚洲购物网站:美国亚米网(Yamibuy)
2020/05/05 全球购物
几道PHP面试题
2013/04/14 面试题
公司活动邀请函
2014/01/24 职场文书
民主生活会对照检查材料
2014/09/22 职场文书
银行转正自我鉴定
2014/09/29 职场文书
领导干部作风建设剖析材料
2014/10/11 职场文书
python numpy中multiply与*及matul 的区别说明
2021/05/26 Python
详解Go语言中配置文件使用与日志配置
2022/06/01 Golang