2、。在Command模式中就是对变化点的典型封装。在Command模式中有一个关键的抽象类,一般人们命名为Command类。他定义了一个执行操作的接口。它有一个抽象的execute操作。具体的Command子类(继承自Command的具体类)将接收者(Receiver类)作为其一个事例变量,并实现execute操作,指定接收者采取的动作。而接收者有执行该请求所需要的具体信息。 下面举个例子: 先定义关键的Command抽象类(这里也可以用接口):package Command;public abstract class Command.
3、..{ protected 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
4、 Command...{ public ConcreteCommand( Receiver receiver ) ...{ super(receiver);专业资料整理分享WORD文档下载可编辑 }//end ConcreteCommand(...) public void execute() ...{ receiver.action(); }//end execute()}//end class ConcreteCommand定义一个Receiver类:
5、package 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 setCommand
6、( Command command ) ...{ this.command = command; }//end setCommand(...) public void executeCommand() ...{ command.execute(); }//end executeCommand() }//end class Invoker最后是调用:package Command;public class CommandPattern专业资料整理分享WORD文档
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