最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年03月02日 11:50:37   作者:hitzjm  
這篇文章主要介紹了Python設(shè)計(jì)模式編程之橋接模式的運(yùn)用,橋接模式主張把抽象部分與它的實(shí)現(xiàn)部分分離,需要的朋友可以參考下

我們先來看一個(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é)出類圖是這樣的: 

201632114505183.gif (678×256)

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

下面我們再來看一個(gè)實(shí)例:

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

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

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

類圖:

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

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

201632114549246.jpg (201×60)

相關(guān)文章

  • pyTorch深入學(xué)習(xí)梯度和Linear Regression實(shí)現(xiàn)

    pyTorch深入學(xué)習(xí)梯度和Linear Regression實(shí)現(xiàn)

    這篇文章主要介紹了pyTorch深入學(xué)習(xí),實(shí)現(xiàn)梯度和Linear Regression,文中呈現(xiàn)了詳細(xì)的示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-09-09
  • Python3遠(yuǎn)程監(jiān)控程序的實(shí)現(xiàn)方法

    Python3遠(yuǎn)程監(jiān)控程序的實(shí)現(xiàn)方法

    今天小編就為大家分享一篇Python3遠(yuǎn)程監(jiān)控程序的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python中的變量、運(yùn)算符與流程控制

    Python中的變量、運(yùn)算符與流程控制

    本文詳細(xì)講解了Python中的變量、運(yùn)算符與流程控制,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • python使用calendar輸出指定年份全年日歷的方法

    python使用calendar輸出指定年份全年日歷的方法

    這篇文章主要介紹了python使用calendar輸出指定年份全年日歷的方法,涉及Python使用calendar模塊操作日期的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • python 普通克里金(Kriging)法的實(shí)現(xiàn)

    python 普通克里金(Kriging)法的實(shí)現(xiàn)

    這篇文章主要介紹了python 普通克里金(Kriging)法的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • 如何寫好?Python?的?Lambda?函數(shù)

    如何寫好?Python?的?Lambda?函數(shù)

    這篇文章主要介紹了如何寫好?Python?的?Lambda?函數(shù),Lambda?函數(shù)是?Python?中的匿名函數(shù),下面文章通過介紹Lambda?函數(shù)的相關(guān)內(nèi)容展開文章主題,需要的小伙伴可以參考一下
    2022-03-03
  • 利用Python自動生成PPT的示例詳解

    利用Python自動生成PPT的示例詳解

    在日常工作中,PPT制作是常見的工作。這篇文章主要為大家詳細(xì)介紹了如何利用Python自動生成PPT,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-07-07
  • Python使用pycharm導(dǎo)入pymysql教程

    Python使用pycharm導(dǎo)入pymysql教程

    這篇文章主要介紹了Python使用pycharm導(dǎo)入pymysql教程,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • OpenCV實(shí)現(xiàn)對象跟蹤的方法

    OpenCV實(shí)現(xiàn)對象跟蹤的方法

    OpenCV 是一個(gè)很好的處理圖像和視頻的工具,本文主要介紹了OpenCV 進(jìn)行對象跟蹤,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • python爬蟲可以爬什么

    python爬蟲可以爬什么

    在本篇文章里小編給大家整理的是關(guān)于python爬蟲的作用地方以及相關(guān)知識點(diǎn),需要的朋友們可以學(xué)習(xí)下。
    2020-06-06

最新評論

康保县| 西青区| 海原县| 泽普县| 开远市| 黄浦区| 寿宁县| 巴南区| 神农架林区| 屯门区| 改则县| 福安市| 会同县| 井陉县| 三台县| 龙山县| 读书| 广河县| 德钦县| 永春县| 龙门县| 偏关县| 津市市| 玉树县| 五台县| 沾化县| 宜兰市| 呼和浩特市| 乌鲁木齐县| 扎赉特旗| 桂林市| 西吉县| 三明市| 堆龙德庆县| 敦化市| 黔南| 榆社县| 汝城县| 洪湖市| 宜城市| 交城县|