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 完成工作 ...