2、对变化点的典型封装。在Command模式中有一个关键的抽象类,一般人们命名为Command类。他定义了一个执行操作的接口。它有一个抽象的execute操作。具体的Command子类(继承自Command的具体类)将接收者(Receiver类)作为其一个事例变量,并实现execute操作,指定接收者采取的动作。而接收者有执行该请求所需要的具体信息。 下面举个例子: 先定义关键的Command抽象类(这里也可以用接口):package Command;public abstract class Command...{ protected
3、Receiver receiver; public Command(Receiver receiver) ...{ this.receiver = receiver; }//end Command(...) abstract public void execute(); }//end abstract class Command再继承一个具体类:package Command;public class ConcreteCommand extends Command...{
4、public ConcreteCommand( Receiver receiver ) ...{ super(receiver); }//end ConcreteCommand(...) public void execute() ...{ receiver.action(); }//end execute()}//end class ConcreteCommand定义一个Receiver类:package Command;public class Receiver...{
5、 public void action() ...{ System.out.println("Receiver.Action()"); }//end Action() }//end class Receiver定义一个Invoker类:package Command;class Invoker ...{ private Command command; public void setCommand( Command command ) ...{ this.com
6、mand = command; }//end setCommand(...) public void executeCommand() ...{ command.execute(); }//end executeCommand() }//end class Invoker最后是调用:package Command;public class CommandPattern...{ Receiver rceiver = new Receiver(); Command command
7、 = new ConcreteCommand(rceiver); Invoker invoker = new Invoker(); /** *//** Creates a new instance of CommandPattern */ public CommandPattern() ...{ }//end CommandPattern public void showCommandPattern() ...{ invoker.setCommand(comm
8、and); invoker.executeCommand(); }//end showCommandPattern() public stati