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

 更新时间:2016年03月02日 11:50:37   作者:hitzjm  
这篇文章主要介绍了Python设计模式编程之桥接模式的运用,桥接模式主张把抽象部分与它的实现部分分离,需要的朋友可以参考下

我们先来看一个例子:

#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();

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

201632114505183.gif (678×256)

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

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

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

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

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

类图:

201632114527959.jpg (614×251)

# -*- 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()

运行结果

201632114549246.jpg (201×60)

相关文章

  • pyTorch深入学习梯度和Linear Regression实现

    pyTorch深入学习梯度和Linear Regression实现

    这篇文章主要介绍了pyTorch深入学习,实现梯度和Linear Regression,文中呈现了详细的示例代码,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2021-09-09
  • Python3远程监控程序的实现方法

    Python3远程监控程序的实现方法

    今天小编就为大家分享一篇Python3远程监控程序的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • Python中的变量、运算符与流程控制

    Python中的变量、运算符与流程控制

    本文详细讲解了Python中的变量、运算符与流程控制,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • python使用calendar输出指定年份全年日历的方法

    python使用calendar输出指定年份全年日历的方法

    这篇文章主要介绍了python使用calendar输出指定年份全年日历的方法,涉及Python使用calendar模块操作日期的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • python 普通克里金(Kriging)法的实现

    python 普通克里金(Kriging)法的实现

    这篇文章主要介绍了python 普通克里金(Kriging)法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • 如何写好 Python 的 Lambda 函数

    如何写好 Python 的 Lambda 函数

    这篇文章主要介绍了如何写好 Python 的 Lambda 函数,Lambda 函数是 Python 中的匿名函数,下面文章通过介绍Lambda 函数的相关内容展开文章主题,需要的小伙伴可以参考一下
    2022-03-03
  • 利用Python自动生成PPT的示例详解

    利用Python自动生成PPT的示例详解

    在日常工作中,PPT制作是常见的工作。这篇文章主要为大家详细介绍了如何利用Python自动生成PPT,文中的示例代码讲解详细,感兴趣的可以了解一下
    2022-07-07
  • Python使用pycharm导入pymysql教程

    Python使用pycharm导入pymysql教程

    这篇文章主要介绍了Python使用pycharm导入pymysql教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • OpenCV实现对象跟踪的方法

    OpenCV实现对象跟踪的方法

    OpenCV 是一个很好的处理图像和视频的工具,本文主要介绍了OpenCV 进行对象跟踪,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • python爬虫可以爬什么

    python爬虫可以爬什么

    在本篇文章里小编给大家整理的是关于python爬虫的作用地方以及相关知识点,需要的朋友们可以学习下。
    2020-06-06

最新评论