command pattern
1.定義(http://en.wikipedia.org/wiki/Command_pattern#Java)
In?object-oriented programming, the?command pattern?is a?behavioral?design pattern?in which an object is used to represent and?encapsulate?all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.
Four terms always associated with the command pattern are?command,?receiver,?invoker?and?client. A?command?object has a?receiver?object and invokes a method of the receiver in a way that is specific to that receiver's class. The receiver then does the work. A command object is separately passed to an?invoker?object, which invokes the command, and optionally does bookkeeping about the command execution. Any command object can be passed to the same invoker object. Both an invoker object and several command objects are held by a?client?object. The client contains the decision making about which commands to execute at which points. To execute a command, it passes the command object to the invoker object. See example code below.
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the class of the method or the method parameters. Using an invoker object allows bookkeeping about command executions to be conveniently performed, as well as implementing different modes for commands, which are managed by the invoker object, without the need for the client to be aware of the existence of bookkeeping or modes..
2. uml圖(http://www.th7.cn/Program/java/2012/03/23/65776.shtml)
??
3. 實例(http://www.th7.cn/Program/java/2012/03/23/65776.shtml)
//抽象接收者,定義了每個接收者應該完成的業務邏輯 abstract class AbstractReceiver { public abstract void doJob(); } // 具體接收者01,實現自己真正的業務邏輯 class Receiver01 extends AbstractReceiver { public void doJob() { System.out.println("接收者01 完成工作 .../n"); } } // 具體接收者02,實現自己真正的業務邏輯 class Receiver02 extends AbstractReceiver { public void doJob() { System.out.println("接收者02 完成工作 .../n"); } } 命令類: // 抽象命令類,定義了每個具體命令被執行的入口方法execute() abstract class AbstractCommand { public abstract void execute(); } // 具體命令類01,通過構造函數的參數決定了該命令由哪個接收者執行 class Command01 extends AbstsractCommand { private AbstractReceiver receiver = null; public Command01(AbstractReceiver receiver) { this.receiver = receiver; } public void execute() { System.out.println("命令01 被發布 ..."); this.receiver.doJob(); } } // 具體命令類02,通過構造函數的參數決定了該命令由哪個接收者執行 class Command02 extends AbstractCommand { private AbstractReceiver receiver = null; public Command02(AbstractReceiver receiver) { this.receiver = receiver; } public void execute() { System.out.println("命令02 被發布 ..."); this.receiver.doJob(); } } 調用者類: // 調用者,負責將具體的命令傳送給具體的接收者 class Invoker { private AbstractCommand command = null; public void setCommand(AbstractCommand command) { this.command = command; } public void action() { this.command.execute(); } }測試類
//測試類 public class Client { public static void main(String[] args) { // 創建調用者 Invoker invoker = new Invoker(); // 創建一個具體命令,并指定該命令被執行的具體接收者 AbstractCommand command01 = new Command01(new Receiver01()); // 給調用者發布一個具體命令 invoker.setCommand(command01); // 調用者執行命令,其實是將其傳送給具體的接收者并讓其真正執行 invoker.action(); AbstractCommand command02 = new Command01(new Receiver02()); invoker.setCommand(command02); invoker.action(); } }測試結果
命令01 被發布 ... 接收者01 完成工作 ...命令02 被發布 ... 接收者02 完成工作 ...轉載于:https://www.cnblogs.com/davidwang456/p/3847986.html
總結
以上是生活随笔為你收集整理的command pattern的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Adapter pattern
- 下一篇: JMX 与系统管理--转