我们先来看一个例子:
#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();
可以总结出类图是这样的:
所以,桥接模式的概念在于将系统抽象部分与它的实现部分分离,使它们可以独立地变化。
由于目标系统存在多个角度的分类,每一种分类都会有多种变化,那么就可以把多角度分离出来,让它们独立变化,减少它们之间的耦合。
下面我们再来看一个实例:
基本原理请参考相关书籍,这里直接给实例
假期旅游 从目的地角度可以分为 上海和大连,从方式角度可以分为跟团和独体
桥接模式把这两种分类连接起来可以进行选择。
类图:
# -*- 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,设计模式,桥接模式
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“实例解析Python设计模式编程之桥接模式的运用”评论...
更新动态
2025年11月03日
2025年11月03日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]


