




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
命令模式封裝調用一個例子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);}上述程序設計,當按鈕和菜單項不多的時候工作良好,但按鈕和菜單項多時,就不好辦了。publicinterfaceCommand{ publicvoidExecute();}publicvoidactionPerformed(ActionEvente){ Commandcmd=(Command)e.getSource(); cmd.Execute();}這樣,我們需要給每一個對象提供一個執行的方法。命令模式theseprogramobjectsshouldbecompletelyseparatefromeachotherandshouldnothavetoknowhowotherobjectswork.TheuserinterfacereceivesacommandandtellsaCommandobjecttocarryoutwhateverdutiesithasbeeninstructedtodo.TheUIdoesnotandshouldnotneedtoknowwhattaskswillbeexecuted.命令模式程序對象應該徹底的彼此解耦,不需要知道其他的對象是如何工作的。用戶界面接收到命令然后告訴命令對象執行設定的工作,用戶界面不知道也不應該知道命令是如何執行的。命令的發送者命令的接受者命令對象解耦了命令的發送者和命令的接受者飯店用餐顧客點了飯菜,形成一個訂單,侍者將訂單交給廚房,廚房根據訂單配菜。侍者無需知道訂單的內容。飯菜的制作者和飯菜的消費者完全分開,彼此無需直接聯系。遙控器的例子遙控器需要控制每一個電器的動作,如電燈,電扇,電視機,同是開,動作不一樣。我們設置命令接口
publicinterfaceCommand{publicvoidexecute();}publicclassLightOnCommandimplementsCommand{ Lightlight; publicLightOnCommand(Lightlight){ this.light=light; } publicvoidexecute(){ light.on(); }}命令的接收對象命令接收對象自己執行命令對象接收對象實現了命令接口execute(){receive.action();}
接受者CommandpublicclassSimpleRemoteControl{ Commandslot; publicSimpleRemoteControl(){}
publicvoidsetCommand(Commandcommand){ slot=command; }publicvoidbuttonWasPressed(){ slot.execute(); }}遙控器測試publicclassRemoteControlTest{ publicstaticvoidmain(String[]args){ SimpleRemoteControlremote=newSimpleRemoteControl(); Lightlight=newLight(); LightOnCommandlightOn=newLightOnCommand(light); remote.setCommand(lightOn); remote.buttonWasPressed(); }}命令模式將請求封裝成對象,使用不同的請求、隊列或日志來參數化其他對象。命令模式支持可撤銷操作。將請求封裝成對象,什么對象?對象將接受者和動作包在內部,只有一個execute接口,外部調用這個接口,不知會進行什么樣的操作。多功能遙控器需要控制多個設備,每一個設備都有開關按鈕需要一個命令組,來控制設備組:起居室燈,廚房燈,吊扇,車庫門,音響,等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它是一個空對象,稱監視對象,避免了判斷
if(onCommand[slot]!=null)onCommand[slot].execute();撤銷命令有時應該允許后悔,允許命令撤銷。publicinterfaceCommand{ publicvoidexecute(); publicvoidundo();}publicclassLightOffCommandimplementsCommand{ Lightlight;
publicLightOffCommand(Lightlight){ this.light=light; }
publicvoidexecute(){ light.off(); }
publicvoidundo(){ light.on(); }}使用撤銷命令的遙控器代碼測試代碼使用狀態實現撤銷吊扇代碼加入撤銷到吊扇的命令類代碼其它幾個low,medium,off.如何實現。測試吊扇類代碼遙控器的party模式產生一個新的命令,可以讓所有設備打開。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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 危險性較大化工流程工程清單及管理措施
- 2024-2025學年八年級數學學科競賽備賽計劃
- 數據中心機房施工安全保證措施
- 二年級下冊體育與健康考核評價計劃
- 家校共育藝術體驗活動計劃
- 2025年新疆物理高一下期末教學質量檢測模擬試題含解析
- 新時代中小學教師職業行為與師德建設心得體會
- 小學五年級下冊信息技術多樣化教學模式計劃
- 生態文明環境保護教育計劃
- 福建?。掀綇B門福州漳州市)2025屆八年級數學第一學期期末學業水平測試試題含解析
- 2025年廣東高考政治試卷真題答案詳解講評(課件)
- 卡口及道路交通智能監控系統方案設計
- 2025年家庭照護師職業資格考試試題及答案
- 呼吸機相關性肺炎的預防和護理
- 2025年綏化市中考化學試題卷(含答案解析)
- 門診口腔院感基礎知識培訓
- 論詠嘆調《媽媽不在》的形象刻畫與唱段處理
- 危重病人觀察和護理要點
- 砌體工程培訓課件
- GB/T 45719-2025半導體器件金屬氧化物半導體(MOS)晶體管的熱載流子試驗
- 2025-2030中國醫藥商業行業盈利態勢與投資潛力分析報告
評論
0/150
提交評論