package com.ding.shejimoshi.Command;
abstract class Command {
protected Receiver receiver;
public Command (Receiver receiver){
this.receiver=receiver;
}
abstract public void Execute();
}
package com.ding.shejimoshi.Command;
public class Receiver {
public void Action(){
System.out.println("执行请求!");
}
}
package com.ding.shejimoshi.Command;
public class ConcreteCommand extends Command{
public ConcreteCommand (Receiver receiver){
super(receiver);
}
public void Execute(){
receiver.Action();
}
}
package com.ding.shejimoshi.Command;
public class Invoker {
private Command command;
public void SetCommand(Command command){
this.command=command;
}
public void ExecuteCommand(){
command.Execute();
}
}
package com.ding.shejimoshi.Command;
public class CommandMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Receiver r=new Receiver();
Command c=new ConcreteCommand(r);
Invoker i=new Invoker();
i.SetCommand(c);
i.ExecuteCommand();
}
}
优点 有利于顺序的执行,第二 容易记录
第三 容易撤销请求 和可新建请求