深入淺出設(shè)計(jì)模式之命令模式_第1頁(yè)
深入淺出設(shè)計(jì)模式之命令模式_第2頁(yè)
深入淺出設(shè)計(jì)模式之命令模式_第3頁(yè)
深入淺出設(shè)計(jì)模式之命令模式_第4頁(yè)
深入淺出設(shè)計(jì)模式之命令模式_第5頁(yè)
已閱讀5頁(yè),還剩26頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

命令模式封裝調(diào)用一個(gè)例子publicvoidactionPerformed(ActionEvente){ Objectobj=e.getSource(); if(obj==mnuOpen)fileOpen();//openfile if(obj==mnuExit)exitClicked();//exitfromprogram if(obj==btnRed)redClicked();//turnred}privatevoidexitClicked(){System.exit(0);}privatevoidfileOpen(){ FileDialogfDlg=newFileDialog(this,"Openafile",FileDialog.LOAD); fDlg.show();}privatevoidredClicked(){ p.setBackground(Color.red);}上述程序設(shè)計(jì),當(dāng)按鈕和菜單項(xiàng)不多的時(shí)候工作良好,但按鈕和菜單項(xiàng)多時(shí),就不好辦了。publicinterfaceCommand{ publicvoidExecute();}publicvoidactionPerformed(ActionEvente){ Commandcmd=(Command)e.getSource(); cmd.Execute();}這樣,我們需要給每一個(gè)對(duì)象提供一個(gè)執(zhí)行的方法。命令模式theseprogramobjectsshouldbecompletelyseparatefromeachotherandshouldnothavetoknowhowotherobjectswork.TheuserinterfacereceivesacommandandtellsaCommandobjecttocarryoutwhateverdutiesithasbeeninstructedtodo.TheUIdoesnotandshouldnotneedtoknowwhattaskswillbeexecuted.命令模式程序?qū)ο髴?yīng)該徹底的彼此解耦,不需要知道其他的對(duì)象是如何工作的。用戶界面接收到命令然后告訴命令對(duì)象執(zhí)行設(shè)定的工作,用戶界面不知道也不應(yīng)該知道命令是如何執(zhí)行的。命令的發(fā)送者命令的接受者命令對(duì)象解耦了命令的發(fā)送者和命令的接受者飯店用餐顧客點(diǎn)了飯菜,形成一個(gè)訂單,侍者將訂單交給廚房,廚房根據(jù)訂單配菜。侍者無(wú)需知道訂單的內(nèi)容。飯菜的制作者和飯菜的消費(fèi)者完全分開,彼此無(wú)需直接聯(lián)系。遙控器的例子遙控器需要控制每一個(gè)電器的動(dòng)作,如電燈,電扇,電視機(jī),同是開,動(dòng)作不一樣。我們?cè)O(shè)置命令接口

publicinterfaceCommand{publicvoidexecute();}publicclassLightOnCommandimplementsCommand{ Lightlight; publicLightOnCommand(Lightlight){ this.light=light; } publicvoidexecute(){ light.on(); }}命令的接收對(duì)象命令接收對(duì)象自己執(zhí)行命令對(duì)象接收對(duì)象實(shí)現(xiàn)了命令接口execute(){receive.action();}

接受者CommandpublicclassSimpleRemoteControl{ Commandslot; publicSimpleRemoteControl(){}

publicvoidsetCommand(Commandcommand){ slot=command; }publicvoidbuttonWasPressed(){ slot.execute(); }}遙控器測(cè)試publicclassRemoteControlTest{ publicstaticvoidmain(String[]args){ SimpleRemoteControlremote=newSimpleRemoteControl(); Lightlight=newLight(); LightOnCommandlightOn=newLightOnCommand(light); remote.setCommand(lightOn); remote.buttonWasPressed(); }}命令模式將請(qǐng)求封裝成對(duì)象,使用不同的請(qǐng)求、隊(duì)列或日志來參數(shù)化其他對(duì)象。命令模式支持可撤銷操作。將請(qǐng)求封裝成對(duì)象,什么對(duì)象?對(duì)象將接受者和動(dòng)作包在內(nèi)部,只有一個(gè)execute接口,外部調(diào)用這個(gè)接口,不知會(huì)進(jìn)行什么樣的操作。多功能遙控器需要控制多個(gè)設(shè)備,每一個(gè)設(shè)備都有開關(guān)按鈕需要一個(gè)命令組,來控制設(shè)備組:起居室燈,廚房燈,吊扇,車庫(kù)門,音響,等publicclassRemoteControl{ Command[]onCommands; Command[]offCommands;

publicRemoteControl(){ onCommands=newCommand[7]; offCommands=newCommand[7];

CommandnoCommand=newNoCommand(); for(inti=0;i<7;i++){ onCommands[i]=noCommand; offCommands[i]=noCommand; } }

publicvoidsetCommand(intslot,CommandonCommand,CommandoffCommand){ onCommands[slot]=onCommand; offCommands[slot]=offCommand; }publicvoidonButtonWasPushed(intslot){ onCommands[slot].execute(); } publicvoidoffButtonWasPushed(intslot){ offCommands[slot].execute(); }publicclassLightOffCommandimplementsCommand{

??

}NoCommand它是一個(gè)空對(duì)象,稱監(jiān)視對(duì)象,避免了判斷

if(onCommand[slot]!=null)onCommand[slot].execute();撤銷命令有時(shí)應(yīng)該允許后悔,允許命令撤銷。publicinterfaceCommand{ publicvoidexecute(); publicvoidundo();}publicclassLightOffCommandimplementsCommand{ Lightlight;

publicLightOffCommand(Lightlight){ this.light=light; }

publicvoidexecute(){ light.off(); }

publicvoidundo(){ light.on(); }}使用撤銷命令的遙控器代碼測(cè)試代碼使用狀態(tài)實(shí)現(xiàn)撤銷吊扇代碼加入撤銷到吊扇的命令類代碼其它幾個(gè)low,medium,off.如何實(shí)現(xiàn)。測(cè)試吊扇類代碼遙控器的party模式產(chǎn)生一個(gè)新的命令,可以讓所有設(shè)備打開。publicclassMacroCommandimplementsCommand{ Command[]commands; publicMacroCommand(Command[]commands){ mands=commands; } publicvoidexecute(){ for(inti=0;i<commands.length;i++){ commands[i].execute(); } } publicvoidundo(){ for(inti=0;i<commands.length;i++){ commands

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論