我们先来看一个例子:
#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 pision
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()
运行结果