国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

實(shí)例解析Python設(shè)計(jì)模式編程之橋接模式的運(yùn)用

2020-01-04 17:40:33
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了Python設(shè)計(jì)模式編程之橋接模式的運(yùn)用,橋接模式主張把抽象部分與它的實(shí)現(xiàn)部分分離,需要的朋友可以參考下
 

我們先來(lái)看一個(gè)例子:

#encoding=utf-8 # #by panda #橋接模式  def printInfo(info):   print unicode(info, 'utf-8').encode('gbk')  #抽象類:手機(jī)品牌 class HandsetBrand():   soft = None   def SetHandsetSoft(self, soft):     self.soft = soft      def Run(self):     pass    #具體抽象類:手機(jī)品牌1 class HandsetBrand1(HandsetBrand):   def Run(self):     printInfo('手機(jī)品牌1:')     self.soft.Run()  #具體抽象類:手機(jī)品牌2 class HandsetBrand2(HandsetBrand):   def Run(self):     printInfo('手機(jī)品牌2:')     self.soft.Run()     #功能類:手機(jī)軟件 class HandsetSoft():   def Run(self):     pass  #具體功能類:游戲   class HandsetGame(HandsetSoft):   def Run(self):     printInfo('運(yùn)行手機(jī)游戲')      #具體功能類:通訊錄   class HandsetAddressList(HandsetSoft):   def Run(self):     printInfo('運(yùn)行手機(jī)通信錄')  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();

可以總結(jié)出類圖是這樣的:  

實(shí)例解析Python設(shè)計(jì)模式編程之橋接模式的運(yùn)用

 

所以,橋接模式的概念在于將系統(tǒng)抽象部分與它的實(shí)現(xiàn)部分分離,使它們可以獨(dú)立地變化。
由于目標(biāo)系統(tǒng)存在多個(gè)角度的分類,每一種分類都會(huì)有多種變化,那么就可以把多角度分離出來(lái),讓它們獨(dú)立變化,減少它們之間的耦合。

下面我們?cè)賮?lái)看一個(gè)實(shí)例:

基本原理請(qǐng)參考相關(guān)書籍,這里直接給實(shí)例

假期旅游 從目的地角度可以分為 上海和大連,從方式角度可以分為跟團(tuán)和獨(dú)體

橋接模式把這兩種分類連接起來(lái)可以進(jìn)行選擇。

類圖:

實(shí)例解析Python設(shè)計(jì)模式編程之橋接模式的運(yùn)用

 

# -*- 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 divisionfrom __future__ import print_functionfrom __future__ import unicode_literalsfrom 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  passclass 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  passclass Independent(TravelForm):  """This class implements the Implementor interface and defines its concrete  implementation.  """  def __init__(self, form="by myself"):    super(Independent,self).__init__(form)    passclass 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    passclass DaLian(Destination):  """This class extends the interface defined by Abstraction.  """  def __init__(self, info="Go to DaLian "):    super(DaLian,self).__init__(info)    passclass 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()

運(yùn)行結(jié)果

實(shí)例解析Python設(shè)計(jì)模式編程之橋接模式的運(yùn)用

 
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 班戈县| 黄浦区| 柳州市| 宁夏| 成武县| 金山区| 东乡| 凤凰县| 彭水| 双城市| 肥东县| 山西省| 梁平县| 方城县| 达孜县| 呼玛县| 武陟县| 潜山县| 临海市| 鸡泽县| 长白| 凤冈县| 福州市| 黑龙江省| 苏尼特右旗| 连山| 丹江口市| 合江县| 龙岩市| 奈曼旗| 弥渡县| 东乡族自治县| 大丰市| 平远县| 五指山市| 米林县| 武定县| 兴山县| 井研县| 濉溪县| 绍兴市|