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

熱線電話:13121318867

登錄
首頁精彩閱讀詳解Python設(shè)計(jì)模式編程中觀察者模式與策略模式的運(yùn)用
詳解Python設(shè)計(jì)模式編程中觀察者模式與策略模式的運(yùn)用
2018-07-27
收藏

詳解Python設(shè)計(jì)模式編程中觀察者模式與策略模式的運(yùn)用

這篇文章主要介紹了Python設(shè)計(jì)模式編程中觀察者模式與策略模式的運(yùn)用,觀察者模式和策略模式都可以歸類為結(jié)構(gòu)型的設(shè)計(jì)模式,需要的朋友可以參考下
觀察者模式
觀察者模式:又叫發(fā)布訂閱模式,定義了一種一對多的依賴關(guān)系,讓多個(gè)觀察者對象同時(shí)監(jiān)聽某一個(gè)主題對象,這個(gè)主題對象的狀態(tài)發(fā)生變化時(shí),會(huì)通知所有觀察者對象,是他們能自動(dòng)更新自己。
代碼結(jié)構(gòu)    
class Topic(object):
  """主題類。保存所有觀察者實(shí)例的引用,每個(gè)主題都可以有很多觀察者
  可以增加和刪除觀察者"""
  def __init__(self):
    self.obs = []
 
  def Attach(self, ob):
    self.obs.append(ob)
 
  def Detach(self, ob):
    self.obs.remove(ob)
 
  def Notify(self):
    for ob in self.obs:
      ob.Update()
 
class Observer(object):
  """抽象觀察者類,收到主題的變更通知時(shí),更新自己"""
  def Update(self):
    raise NotImplementedError()
 
class ConcreteTopic(object):
  """一個(gè)具體主題"""
  def __init__(self):
    self.state = None
 
  def ChangeState(self, newState):
    self.state = newState
    self.Notify()
 
class ConcreteObserver(object):
  """一個(gè)具體監(jiān)聽類"""
  def __init__(self, topic):
    self.topic = topic
 
  def Update(self):
    print self.topic.state
 
def client():
  topic = ConcreteTopic()
  topic.Attach(ConcreteObserver(topic))
 
  topic.ChangeState('New State')
眾多MQ中間件都是采用這種模式的思想來實(shí)現(xiàn)的。
觀察者模式可以讓主題和觀察者之間解耦,互相之間盡可能少的依賴。不過抽象主題和抽象觀察者之間還是有耦合的。
策略模式
策略模式: 定義了算法家族,分別封裝起來,讓他們之間可以互相替換。此模式讓算法的變化不影響使用算法的客戶。
代碼框架    
class Strategy(object):
  """抽象算法類"""
  def AlgorithmInterface(self):
    raise NotImplementedError()
 
class ConcreteStrategyA(Strategy):
  def AlgorithmInterface(self):
    print '算法A'
 
class ConcreteStrategyB(Strategy):
  def AlgorithmInterface(self):
    print '算法B'
 
class Context(object):
  """上下文,作用就是封裝策略的實(shí)現(xiàn)細(xì)節(jié),用戶只需要知道有哪些策略可用"""
  def __init__(self, strategy):
    # 初始化時(shí)傳入具體的策略實(shí)例
    self.strategy = strategy
 
  def ContextInterface(self):
    # 負(fù)責(zé)調(diào)用具體的策略實(shí)例的接口
    self.strategy.AlgorithmInterface()
 
def client(cond):
  # 策略模式的使用演示
  # 用戶只需要根據(jù)不同的條件,將具體的算法實(shí)現(xiàn)類傳遞給Context,
  # 然后調(diào)用Context暴露給用戶的接口就行了。
  if cond == 'A':
    context = Context(ConcreteStrategyA())
  elif cond == 'B':
    context = Context(ConcreteStrategyB())
 
  result = context.ContextInterface()
策略模式解決那類問題
在回答這個(gè)問題之前,先說下對策略模式的使用方式的感覺。上面的client函數(shù),怎么看起來就像是簡單工廠模式中的工廠函數(shù)呢?確實(shí)如此,實(shí)際上策略模式可以和簡工廠模式結(jié)合起來,將更多細(xì)節(jié)封裝在策略模式內(nèi)部,讓使用者更容易的使用。
那么策略模式和簡單工廠模式有什么不同呢?策略模式中的算法是用來解決同一個(gè)問題的,根據(jù)時(shí)間、條件不同,算法的具體細(xì)節(jié)有差異,但最終解決的是同一個(gè)問題。在需求分析過程中,當(dāng)聽到需要在不同時(shí)間應(yīng)用不同的業(yè)務(wù)規(guī)則,就可以考慮使用策略模式來處理這種變化的可能性。
缺點(diǎn)
使用者需要知道每一種策略的具體含義,并負(fù)責(zé)選擇策略
改進(jìn)
結(jié)合簡單工廠模式,將策略選擇封裝在Context內(nèi)部,解放client:    
class Context(object):
  def __init__(self, cond):
    if cond == 'A':
      self.strategy = Context(ConcreteStrategyA())
    elif cond == 'B':
      self.strategy = Context(ConcreteStrategyB())
 
  def ContextInterface(self):
    self.strategy.AlgorithmInterface()
 
 
def client(cond):
  context = Context(cond)
  result = context.ContextInterface()
改進(jìn)后的遺留問題
每次需要增加新的策略時(shí),就需要修改Context的構(gòu)造函數(shù),增加一個(gè)新的判斷分支。

數(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)的第一個(gè)參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(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){ //倒計(jì)時(shí)完成 $(".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); }