Command抽象命令类
ConcreteCommand具体命令类
public interface Command{ public void execute(); } class ConcreteCommand implements Command{ private Receiver receiver; public ConcreteCommand(Receiver receiver) { super(); this.receiver = receiver; } @Override public void execute() { // TODO Auto-generated method stub receiver.action(); } } /** * 调用者、发起者 */ public class Invoke { private Command command; public Invoke(Command command) { super(); this.command = command; } /*业务方法,用于调用命令的方法*/ public void call(){ command.execute(); } } public class Receiver { public void action(){ System.out.println("action"); } } public class Client { public static void main(String[] args) { // TODO Auto-generated method stub Command command = new ConcreteCommand(new Receiver()); Invoke Invoke = new Invoke(command); Invoke.call(); } }
JAVA设计模式(15):行为型-命令模式(Command)
原文地址:http://blog.csdn.net/lovesomnus/article/details/45601109