99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀實例解析Python設(shè)計模式編程之橋接模式的運用
實例解析Python設(shè)計模式編程之橋接模式的運用
2018-07-24
收藏
這篇文章主要介紹了Python設(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('運行手機(jī)游戲')
      
#具體功能類:通訊錄   
class HandsetAddressList(HandsetSoft):
  def Run(self):
    printInfo('運行手機(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é)出類圖是這樣的:


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

下面我們再來看一個實例:

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

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

橋接模式把這兩種分類連接起來可以進(jì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__importdivision
from__future__importprint_function
from__future__importunicode_literals
fromfuture_builtinsimport*
   
 
classTravelForm(object):
  """This class defines the interface for implementation classes.
  """
  def__init__(self, form="stay at home"):
    self.form=form
    pass
 
  defGetForm(self):
    returnself.form
    pass
  pass
 
classGroup(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
 
classIndependent(TravelForm):
  """This class implements the Implementor interface and defines its concrete
  implementation.
  """
  def__init__(self, form="by myself"):
    super(Independent,self).__init__(form)
    pass
 
classDestination(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
 
  defGetInfo(self):
    # imp->Operation();
    returnprint(self.info+" "+self.form.GetForm())
    pass
 
  defSetForm(self, form):
    self.form=form
    pass
 
classDaLian(Destination):
  """This class extends the interface defined by Abstraction.
  """
  def__init__(self, info="Go to DaLian "):
    super(DaLian,self).__init__(info)
    pass
 
classShangHai(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()

運行結(jié)果


數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }