实例解析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进程类subprocess的一些操作方法例子
Nov 22 Python
python中根据字符串调用函数的实现方法
Jun 12 Python
Python 中urls.py:URL dispatcher(路由配置文件)详解
Mar 24 Python
Python tkinter模块中类继承的三种方式分析
Aug 08 Python
Python中类的初始化特殊方法
Dec 01 Python
Python hashlib模块用法实例分析
Jun 12 Python
python IDLE 背景以及字体大小的修改方法
Jul 12 Python
Django 外键的使用方法详解
Jul 19 Python
python抓取多种类型的页面方法实例
Nov 20 Python
Django全局启用登陆验证login_required的方法
Jun 02 Python
解决python 执行sql语句时所传参数含有单引号的问题
Jun 06 Python
python Yaml、Json、Dict之间的转化
Oct 19 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程序
2006/10/09 PHP
那些年一起学习的PHP(三)
2012/03/22 PHP
PHP json_decode函数详细解析
2014/02/17 PHP
Yii Framework框架获取分类下面的所有子类方法
2014/06/20 PHP
php日期操作技巧小结
2016/06/25 PHP
jquery EasyUI的formatter格式化函数代码
2011/01/12 Javascript
来自国外的30个基于jquery的Web下拉菜单
2012/06/22 Javascript
JavaScript如何禁止Backspace键
2015/12/02 Javascript
JS实现将数字金额转换为大写人民币汉字的方法
2016/08/02 Javascript
AngularJS框架的ng-app指令与自动加载实现方法分析
2017/01/04 Javascript
JSON键值对序列化和反序列化解析
2017/01/24 Javascript
BootStrap selectpicker后台动态绑定数据
2017/06/01 Javascript
微信小程序学习之数据处理详解
2017/07/05 Javascript
前端把html表格生成为excel表格的实例
2017/09/19 Javascript
使用Angular CLI进行单元测试和E2E测试的方法
2018/03/24 Javascript
layer扩展打开/关闭动画的方法
2019/09/23 Javascript
jQuery操作元素追加内容示例
2020/01/10 jQuery
在vue中使用jsonp进行跨域请求接口操作
2020/10/29 Javascript
[49:18]2018DOTA2亚洲邀请赛 3.31 小组赛 A组 OG vs TNC
2018/04/01 DOTA
python批量修改文件后缀示例代码分享
2013/12/24 Python
Python的Flask框架中SQLAlchemy使用时的乱码问题解决
2015/11/07 Python
node.js获取参数的常用方法(总结)
2017/05/29 Python
pandas将DataFrame的列变成行索引的方法
2018/04/10 Python
在pycharm中python切换解释器失败的解决方法
2018/10/29 Python
python编写计算器功能
2019/10/25 Python
python opencv 检测移动物体并截图保存实例
2020/03/10 Python
django model通过字典更新数据实例
2020/04/01 Python
vscode配置anaconda3的方法步骤
2020/08/08 Python
移动端html5 meta标签的神奇功效
2016/01/06 HTML / CSS
解释一下抽象方法和抽象类
2016/08/27 面试题
维德科技C#面试题笔试题
2015/12/09 面试题
二年级语文教学反思
2014/02/02 职场文书
圣诞晚会主持词开场白
2015/05/28 职场文书
MySQL命令无法输入中文问题的解决方式
2021/08/30 MySQL
SpringBoot2零基础到精通之数据库专项精讲
2022/03/22 Java/Android
Go gorilla/sessions库安装使用
2022/08/14 Golang