欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

我們先來看一個例子:

#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)存在多個角度的分類,每一種分類都會有多種變化,那么就可以把多角度分離出來,讓它們獨(dú)立變化,減少它們之間的耦合。

下面我們再來看一個實(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)文章

  • python中xrange和range的區(qū)別

    python中xrange和range的區(qū)別

    這篇文章主要介紹了python中xrange和range的區(qū)別,需要的朋友可以參考下
    2014-05-05
  • PyTorch安裝與基本使用詳解

    PyTorch安裝與基本使用詳解

    這篇文章主要介紹了PyTorch安裝與基本使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 詳解Python中行列式的計算

    詳解Python中行列式的計算

    矩陣的行列式是僅與方陣相關(guān)的標(biāo)量,?這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)行列式的計算,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11
  • Python著名游戲?qū)崙?zhàn)之方塊連接 我的世界

    Python著名游戲?qū)崙?zhàn)之方塊連接 我的世界

    讀萬卷書不如行萬里路,學(xué)的扎不扎實(shí)要通過實(shí)戰(zhàn)才能看出來,本篇文章手把手帶你模仿著名游戲——我的世界,大家可以在過程中查缺補(bǔ)漏,看看自己掌握程度怎么樣
    2021-10-10
  • python 用for循環(huán)實(shí)現(xiàn)1~n求和的實(shí)例

    python 用for循環(huán)實(shí)現(xiàn)1~n求和的實(shí)例

    今天小編就為大家分享一篇python 用for循環(huán)實(shí)現(xiàn)1~n求和的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Python:type、object、class與內(nèi)置類型實(shí)例

    Python:type、object、class與內(nèi)置類型實(shí)例

    今天小編就為大家分享一篇Python:type、object、class與內(nèi)置類型實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Django 聚合函數(shù)的具體使用

    Django 聚合函數(shù)的具體使用

    orm模型中的聚合函數(shù)跟MySQL中的聚合函數(shù)作用是一致的,也有像Sum、Avg、Count、Max、Min,接下來我們逐個介紹,下面就一起來了解一下
    2021-05-05
  • mat矩陣和npy矩陣實(shí)現(xiàn)互相轉(zhuǎn)換(python和matlab)

    mat矩陣和npy矩陣實(shí)現(xiàn)互相轉(zhuǎn)換(python和matlab)

    這篇文章主要介紹了mat矩陣和npy矩陣實(shí)現(xiàn)互相轉(zhuǎn)換(python和matlab),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • pandas中的ExcelWriter和ExcelFile的實(shí)現(xiàn)方法

    pandas中的ExcelWriter和ExcelFile的實(shí)現(xiàn)方法

    這篇文章主要介紹了pandas中的ExcelWriter和ExcelFile的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • python人工智能使用RepVgg實(shí)現(xiàn)圖像分類示例詳解

    python人工智能使用RepVgg實(shí)現(xiàn)圖像分類示例詳解

    這篇文章主要介紹了python人工智能使用RepVgg實(shí)現(xiàn)圖像分類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10

最新評論