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