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

熱線電話:13121318867

登錄
首頁精彩閱讀Python設(shè)計模式之命令模式簡單示例
Python設(shè)計模式之命令模式簡單示例
2018-07-27
收藏

Python設(shè)計模式之命令模式簡單示例

本文實例講述了Python設(shè)計模式之命令模式。分享給大家供大家參考,具體如下:

命令模式介紹:

在面向?qū)ο缶幊讨?,命令模式是概括所有方法信息的設(shè)計模式。

此模式對象包涵方法名,及其相關(guān)參數(shù)值。

命令模式是一個分類的觀察者設(shè)計模式,在命令模式下,對象被概括為一個命令表單,此表單包涵了所有用戶需要的方法。

舉個例子:如果有個按鈕是用戶接口“red”,當(dāng)被觸碰的時候,會啟動后臺的“turn red”接口?,F(xiàn)在用戶并不知道,通過什么類或者方法的接口能夠讓后臺處理“turn red”操作,但是這個命令被發(fā)送了(觸碰“red”按鈕),會使得后臺處理“turn red”操作。因此,命令模式給用戶一個接口,而不用讓用戶了解哪些是實際執(zhí)行的程序,也不會影響到用戶程序。

實現(xiàn)命令模式的關(guān)鍵就是讓調(diào)用者不要包涵底層實際命令執(zhí)行代碼,相同的調(diào)用者應(yīng)該采用相同的接口。

命令模式是由三個組件構(gòu)成,客戶,調(diào)用者,接受者。

客戶:一個實例化的對象

調(diào)用者:決定哪個方法被調(diào)用

接受者:實際命令的執(zhí)行者

Example:

實現(xiàn)一個開關(guān)
切換ON/OFF
用開關(guān)ON/OFF去硬編碼一個事件

代碼如下:

class Switch:
 ''' The INVOKER class'''
 def __init__(self, flipUpCmd, flipDownCmd):
  self.__flipUpCommand = flipUpCmd
  self.__flipDownCommand = flipDownCmd
 def flipUp(self):
  self.__flipUpCommand.execute()
 def flipDown(self):
  self.__flipDownCommand.execute()
class Light:
 '''The RECEIVER Class'''
 def turnOn(self):
  print "The light is on"
 def turnOff(self):
  print "The light is off"
class Command:
 """The Command Abstrace class"""
 def __init__(self):
  pass
 def execute(self):
  pass
class FlipUpCommand(Command):
 '''The Command class for turning on the light'''
 def __init__(self, light):
  self.__light = light
 def execute(self):
  self.__light.turnOn()
class FileDownCommand(Command):
 '''The Command class for turning off the light'''
 def __init__(self, light):
  Command.__init__(self)
  self.__light = light
 def execute(self):
  self.__light.turnOff()
class LightSwitch:
 '''The Client Class'''
 def __init__(self):
  self.__lamp = Light()
  self.__switchUp = FlipUpCommand(self.__lamp)
  self.__switchDown = FileDownCommand(self.__lamp)
  self.__switch = Switch(self.__switchUp, self.__switchDown)
 def switch(self, cmd):
  cmd = cmd.strip().upper()
  try:
   if cmd == "ON":
    self.__switch.flipUp()
   elif cmd == "OFF":
    self.__switch.flipDown()
   else:
    print "Argument \"ON\" or \"OFF\" is required"
  except Exception,msg:
   print "Exception occured:%s" % msg
#Execute if the file is run as a script and not imported as a module
if __name__ == "__main__":
 lightSwitch = LightSwitch()
 print "Switch ON test"
 lightSwitch.switch("ON")
 print "Switch OFF test"
 lightSwitch.switch("OFF")
 print "Invalid Command test"
 lightSwitch.switch("****")

運行結(jié)果:

總結(jié):面向?qū)ο蟮姆椒?,就是這么牛叉啊,代碼看得讓人頭暈,層層的封裝。警惕面向?qū)ο缶幊痰倪^度對象化。


數(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); }