編輯:關於android開發
命令模式的定義:將“請求”封裝成對象,以便使用不同的請求、隊列或者日志來參數化其他對象。命令模式也支持撤銷的操作。注意命令模式是將請求封裝成對象!
其實簡單的說,命令模式就是把方法調用封裝起來了,通過封裝方法調用,可以把運算塊包裝成型,所以調用此運算的對象不需要關心事情是如何進行的,只要知道如何使用包裝成型的方法來完成它就可以。
命令模式的使用場景:當需要將發出請求的對象和執行請求的對象解耦的時候,請考慮使用命令模式吧。
場景舉例:設計一款多功能遙控器,該遙控器可以控制家裡各個電器的狀態,例如電燈的開關,電視的開關,電扇的速度,車庫門的開關等等,下面使用命令模式實現這個遙控器。在這裡,發出請求的對象是遙控器,執行請求的對象是各種電器。
首先需要讓所有的命令對象實現同一個命令接口
public interface Command { public void execute(); public void undo(); }
接口中有兩個方法,一個是執行動作的方法execute,一個是撤銷動作的方法undo;
下面實現電燈打開關閉命令前先聲明一個電燈類
public class Light { protected void on() { System.out.println("light is on"); } protected void off() { System.out.println("light is off"); } }
電燈類只有兩個方法on—>打開,off—>關閉;然後實現打開電燈的命令
public class LightOnCommand implements Command { Light light; public LightOnCommand(Light light) { // TODO Auto-generated constructor stub this.light = light; } @Override public void execute() { // TODO Auto-generated method stub light.on(); } @Override public void undo() { // TODO Auto-generated method stub light.off(); } }
注意undo的操作正好是與execute的操作相反的;在寫一個電燈關閉的命令
public class LightOffCommand implements Command { Light light; public LightOffCommand(Light light) { // TODO Auto-generated constructor stub this.light = light; } @Override public void execute() { // TODO Auto-generated method stub light.off(); } @Override public void undo() { // TODO Auto-generated method stub light.on(); } }
再增加一個車庫門的開啟和關閉
public class GarageDoor { public void up() { System.out.println("the garagedoor is up!"); } public void down() { System.out.println("the garagedoor is down!"); } public void lightOn() { System.out.println("the garagedoor's light is on!"); } public void lightOff() { System.out.println("the garagedoor's light is off!"); } }
public class GarageDoorOpenCommand implements Command { GarageDoor garageDoor; public GarageDoorOpenCommand(GarageDoor garageDoor) { // TODO Auto-generated constructor stub this.garageDoor = garageDoor; } @Override public void execute() { // TODO Auto-generated method stub garageDoor.up(); garageDoor.lightOn(); } @Override public void undo() { // TODO Auto-generated method stub garageDoor.down(); garageDoor.lightOff(); } }
public class GarageDoorCloseCommand implements Command { GarageDoor garageDoor; public GarageDoorCloseCommand(GarageDoor garageDoor) { // TODO Auto-generated constructor stub this.garageDoor = garageDoor; } @Override public void execute() { // TODO Auto-generated method stub garageDoor.down(); garageDoor.lightOff(); } @Override public void undo() { // TODO Auto-generated method stub garageDoor.up(); garageDoor.lightOn(); } }
然後就是遙控器的實現了
public class RemoteControl { /* * 由於對應多個電器這裡采用數組記錄這些命令 */ Command[] onCommands; Command[] offCommands; Command undoCommand;//用來記錄撤銷命令 public RemoteControl() { // TODO Auto-generated constructor stub onCommands = new Command[7]; offCommands = new Command[7]; Command noCommand = new NoCommand();//初始化是都是空命令 for (int i = 0; i < 7; i++) { onCommands[i] = noCommand; offCommands[i] = noCommand; } undoCommand=noCommand; } public void setCommand(int slot, Command onCommand, Command offCommand) { onCommands[slot] = onCommand; offCommands[slot] = offCommand; } public void onButtonWasPushed(int slot) { onCommands[slot].execute(); undoCommand=onCommands[slot]; } public void offButtonWasPushed(int slot) { offCommands[slot].execute(); undoCommand=offCommands[slot]; } public void undoButtonWasPushed(){ undoCommand.undo(); } }
空命令在這裡什麼也不做,只是保證上的按鈕均有命令對象存在,空命令的實現
public class NoCommand implements Command { @Override public void execute() { // TODO Auto-generated method stub System.out.println("no command!"); } @Override public void undo() { // TODO Auto-generated method stub } }
開始測試一下遙控器
public class RemoteLoader { public static void main(String[] args) { // TODO Auto-generated method stub RemoteControl remoteControl=new RemoteControl(); Light light=new Light(); LightOnCommand lightOn=new LightOnCommand(light); LightOffCommand lightOff =new LightOffCommand(light); GarageDoor garageDoor=new GarageDoor(); GarageDoorOpenCommand garageDoorOpen=new GarageDoorOpenCommand(garageDoor); GarageDoorCloseCommand garageDoorClose=new GarageDoorCloseCommand(garageDoor); remoteControl.setCommand(0, lightOn, lightOff); remoteControl.setCommand(1, garageDoorOpen, garageDoorClose); remoteControl.onButtonWasPushed(0); remoteControl.onButtonWasPushed(1); remoteControl.offButtonWasPushed(0); remoteControl.offButtonWasPushed(1); } }
打印結果:
light is on the garagedoor is up! the garagedoor's light is on! light is off the garagedoor is down! the garagedoor's light is off!
遙控器實現完畢,測試一下撤銷命令
public class RemoteLoader { public static void main(String[] args) { // TODO Auto-generated method stub RemoteControl remoteControl=new RemoteControl(); Light light=new Light(); LightOnCommand lightOn=new LightOnCommand(light); LightOffCommand lightOff =new LightOffCommand(light); remoteControl.setCommand(0, lightOn, lightOff); remoteControl.onButtonWasPushed(0); System.out.println("****************"); remoteControl.undoButtonWasPushed(); } }
打印結果
light is on **************** light is off
總結:
(1)命令模式將發出請求的對象和執行請求的對象解耦;
(2)在被解耦的兩者之間是通過命令對象進行溝通的。命令對象封裝了接受者和一個或一組動作。
(3)調用者通過調用命令對象的execute()發出請求,這會使得接受者的動作被調用。
(4)調用者可以接受命令當作參數,甚至在運行時動態的進行。
(5)命令可以支持撤銷,做法是實現一個undo()方法來回到上一個execute()被執行前的狀態。
Android--自定義荷載進度的兩種方法,android--荷載博客撰寫人:It一zhai男 轉載請標明地址:http://www.cnblogs.com/i
淺談Android應用保護(一):Android應用逆向的基本方法,淺談android對於未進行保護的Android應用,有很多方法和思路對其進行逆向分析和攻擊。使用一些
Android 扒開美女衣服,android扒開美女本文主要實現一個小的扒開美女衣服的游戲項目 效果如下: 項目布局設計: <FrameLayout xmlns
android常犯錯誤記錄,android犯錯誤記錄Error:Error: Found item Attr/border_width more than one tim