实例解析Python设计模式编程之桥接模式的运用
admin
2023-07-31 02:31:17
0

我们先来看一个例子:

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

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
项目管理和工程管理的区别 项目管理 项目管理,顾名思义就是专注于开发和完成项目的管理,以实现目标并满足成功标准和项目要求。 工...