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

下載本文檔

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

文檔簡介

1、Head First 設(shè)計(jì)模式之命令模式前言: 本章會將封裝帶入到一個(gè)全新的境界,把方法調(diào)用封裝起來。通過封裝方法調(diào)用,把運(yùn)算塊包裝成形。調(diào)用此運(yùn)算的對象不需要知道事情是如何進(jìn)行的,只要知道如何使用包裝形成的方法來完成它就ok了。1 現(xiàn)實(shí)場景應(yīng)用現(xiàn)在有一個(gè)遙控器,該遙控器有7個(gè)插槽需要編程,可以在每個(gè)插槽上放上不同的裝置,然后用按鈕控制它,這七個(gè)插槽具備各自的“開”“關(guān)”按鈕,還有一個(gè)整體用的撤銷按鈕,會撤銷最后一個(gè)按鈕的動作。1.1 創(chuàng)建第一個(gè)命令對象1.1.1 定義命令接口csharp view plain copy 在CODE上查看代碼片派生到我的代碼片public interfaceC

2、ommand void Execute(); 1.1.2 實(shí)現(xiàn)一個(gè)打開燈的命令csharp view plain copy 在CODE上查看代碼片派生到我的代碼片publicclass Light /電燈類 public void On() System.Console.WriteLine("Light is On !"); public void Off() System.Console.WriteLine("Light is Off !"); public class LightOnCommand : Command/實(shí)現(xiàn)開燈命令 private L

3、ight light; public LightOnCommand(Light light) this.light = light; public void Execute() light.On(); 1.1.3 使用命令對象csharp view plain copy 在CODE上查看代碼片派生到我的代碼片public class LightControl privateCommand soft; publicvoid SetCommand(Command cmd) soft= cmd;/設(shè)置命令對象 publicvoid ButtonWasPressed() soft.Execute();

4、/調(diào)用命令對象的方法 1.1.4 簡單測試csharp view plain copy 在CODE上查看代碼片派生到我的代碼片LightControl lightControl = new LightControl();/模擬命令的調(diào)用者 Lightlight = new Light();/創(chuàng)建一個(gè)電燈對象,作為命令的接收者 LightOnCommand lightOnCommand = new LightOnCommand(light);/創(chuàng)建一個(gè)命令,并將接受者傳遞給它 lightControl.SetCommand(lightOnCommand);/將命令傳給調(diào)用者 lightContr

5、ol.ButtonWasPressed();/模擬觸發(fā)按鈕 1.2 實(shí)現(xiàn)遙控器csharp view plain copy 在CODE上查看代碼片派生到我的代碼片public class RemoteControl Command onCommands;/定義打開的命令數(shù)組 Command offCommands; /定義關(guān)閉的命令數(shù)組 public RemoteControl() onCommands = new Command7; offCommands = new Command7; Command noCommand = newNoCommand(); for (int i = 0;

6、i < 7; i+) onCommandsi = noCommand;/初始化命令數(shù)組(默認(rèn)設(shè)置為無命令) offCommandsi = noCommand; /將命令設(shè)置到對應(yīng)的控制器 public void SetCommand(int index,Command onCommand, Command offCommand) onCommandsindex = onCommand; offCommandsindex = offCommand; /觸發(fā)打開控制器 public void OnButtonWasPressed(int index) onCommandsindex.Exec

7、ute(); /觸發(fā)關(guān)閉控制器 public void OffButtonWasPressed(intindex) offCommandsindex.Execute(); public override string ToString() StringBuilder str = newStringBuilder(); str.Append("n-RemoteControl -n"); for (int i = 0; i <onCommands.Length; i+) str.Append("solt" +i + "" + onC

8、ommandsi.GetType().FullName + " " +offCommandsi.GetType().FullName + "n"); return str.ToString(); 1.3 實(shí)現(xiàn)其他控制器csharp view plain copy 在CODE上查看代碼片派生到我的代碼片 /關(guān)閉電燈命令 public classLightOffCommand : Command privateLight light; publicLightOffCommand(Light light) this.light = light; publicv

9、oid Execute() light.Off(); publicvoid Undo() light.On(); /打開電扇命令 public class CeilingFanOnCommand : Command CeilingFan ceilingFan; intpreSpeed; publicCeilingFanOnCommand(CeilingFan ceilingFan) this.ceilingFan = ceilingFan; publicvoid Execute() ceilingFan.On(); publicvoid Undo() ceilingFan.Off(); /關(guān)閉

10、電扇命令 public classCeilingFanOffCommand : Command CeilingFan ceilingFan; publicCeilingFanOffCommand(CeilingFan ceilingFan) this.ceilingFan = ceilingFan; publicvoid Execute() ceilingFan.Off(); publicvoid Undo() ceilingFan.On(); /打開車庫門命令 public classGarageDoorOnCommand : Command GarageDoor garageDoor; p

11、ublicGarageDoorOnCommand(GarageDoor garageDoor) this.garageDoor= garageDoor; publicvoid Execute() garageDoor.On(); publicvoid Undo() garageDoor.Off(); /關(guān)閉車庫門命令 public classGarageDoorOffCommand : Command GarageDoor garageDoor; publicGarageDoorOffCommand(GarageDoor garageDoor) this.garageDoor = garage

12、Door; publicvoid Execute() garageDoor.Off(); publicvoid Undo() garageDoor.On(); /打開CD命令 public classStereCDOnCommand : Command StereCDstereCD; publicStereCDOnCommand(StereCD stereCD) this.stereCD = stereCD; publicvoid Execute() stereCD.On(); publicvoid Undo() stereCD.Off(); /關(guān)閉CD命令 public classStere

13、CDOffCommand : Command StereCDstereCD; publicStereCDOffCommand(StereCD stereCD) this.stereCD = stereCD; publicvoid Execute() stereCD.Off(); publicvoid Undo() stereCD.On(); 1.4簡單測試csharp view plain copy 在CODE上查看代碼片派生到我的代碼片RemoteControlremoteControl = new RemoteControl (); CeilingFan ceilingFan = newC

14、eilingFan("Living Room");/創(chuàng)建電扇對象 GarageDoor garageDoor = newGarageDoor();/創(chuàng)建車庫門對象 StereCD stereCD = new StereCD();/創(chuàng)建CD對象 Light light = new Light();/創(chuàng)建電燈對象 LightOnCommand lightOnCommand = newLightOnCommand(light);/創(chuàng)建開燈命令 LightOffCommand lightOffCommand =new LightOffCommand(light); /創(chuàng)建關(guān)燈命令

15、CeilingFanOnCommandceilingFanOn = new CeilingFanOnCommand(ceilingFan); /創(chuàng)建開電扇命令 CeilingFanOffCommand ceilingFanOff= new CeilingFanOffCommand(ceilingFan);/創(chuàng)建關(guān)電扇命令 GarageDoorOnCommand garageDoorOn =new GarageDoorOnCommand(garageDoor);/創(chuàng)建打開電扇命令 GarageDoorOffCommand garageDoorOff= new GarageDoorOffComma

16、nd(garageDoor);/創(chuàng)建關(guān)閉電扇命令 StereCDOnCommand stereCDOn = newStereCDOnCommand(stereCD);/創(chuàng)建打開CD命令 StereCDOffCommand stereCDOff = newStereCDOffCommand(stereCD);/創(chuàng)建關(guān)閉CD命令 remoteControl.SetCommand(0,lightOnCommand, lightOffCommand);/將電燈命令設(shè)置到對應(yīng)的控制器上 Control.SetCommand(1,ceilingFanOn, ceilingFanOff); /將電燈命令設(shè)置

17、到對應(yīng)的控制器上 remoteControl.SetCommand(2,garageDoorOn, garageDoorOff); /將車庫門命令設(shè)置到對應(yīng)的控制器上 remoteControl.SetCommand(3,stereCDOn, stereCDOff); /將CD命令設(shè)置到對應(yīng)的控制器上 remoteControl.OnButtonWasPressed(0); remoteControl.OffButtonWasPressed(0); remoteControl.OnButtonWasPressed(1); remoteControl.OffButtonWasPressed(1)

18、; remoteControl.OnButtonWasPressed(2); remoteControl.OffButtonWasPressed(2); remoteControl.OnButtonWasPressed(3); remoteControl.OffButtonWasPressed(3); 1.5 實(shí)現(xiàn)撤銷命令1.5.1 修改命令接口,新增Undo()方法csharp view plain copy 在CODE上查看代碼片派生到我的代碼片public interfaceCommand void Execute(); void Undo();/新增撤銷方法 1.5.2 修改命令,實(shí)現(xiàn)

19、Undo方法csharp view plain copy 在CODE上查看代碼片派生到我的代碼片public class LightOnCommand : Command private Light light; public LightOnCommand(Light light) this.light = light; public void Execute() light.On(); public void Undo()/實(shí)現(xiàn)Undo方法 light.Off(); 其他類都依次修改1.5.3 修改RemoteControl類,新增Command對象記錄上一步操作csharp view pl

20、ain copy 在CODE上查看代碼片派生到我的代碼片public class RemoteControlWithUndo Command onCommands; Command offCommands; CommandundoCommand;/創(chuàng)建Command對象用來記錄上一步執(zhí)行的命令 publicRemoteControlWithUndo() onCommands = new Command7; offCommands = new Command7; Command noCommand = new NoCommand(); for(int i = 0; i < 7; i+) o

21、nCommandsi = noCommand; offCommandsi = noCommand; undoCommand = noCommand; . publicvoid OnButtonWasPressed(int index) onCommandsindex.Execute(); undoCommand = onCommandsindex;/記錄打開命令 publicvoid OffButtonWasPressed(int index) offCommandsindex.Execute(); undoCommand = offCommandsindex;/記錄關(guān)閉命令 publicvo

22、id UndoButtonWasPressed()/執(zhí)行撤銷 undoCommand.Undo(); 1.6 宏命令讓遙控器具有Party模式,即一個(gè)按鍵能夠同步調(diào)用多個(gè)命令,這就是所謂的宏命令。 csharp view plain copy 在CODE上查看代碼片派生到我的代碼片public class MacroCommand : Command Command commands;/定義命令數(shù)組,用來接收傳入的命令 public MacroCommand(Command commands) mands = commands; public void Execute()/批量處理多個(gè)命令(即初始化的時(shí)候傳入的命令數(shù)組) for (int i = 0; i <commands.Length; i+) commandsi.Execute(); public void Undo() for (int i = 0; i <

溫馨提示

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

最新文檔

評論

0/150

提交評論