命令模式

来源:互联网 发布:java excel jxl poi 编辑:程序博客网 时间:2024/06/07 18:52





package Command;/*** * 真正命令的执行者 * @author zw * */public class Receiver {public void action() {System.out.println("执行动作");}}



package Command;public interface Command {void execute();}class ConcreteCommand implements Command{private Receiver receiver;public ConcreteCommand(Receiver receiver) {super();this.receiver = receiver;}@Overridepublic void execute() {// TODO Auto-generated method stubreceiver.action();}}


package Command;/*** * 调用者和发起者 * @author zw * */public class Invoke {private Command command;public Invoke(Command command) {super();this.command = command;}public void call() {command.execute();}}

package Command;public class Client {public static void main(String[] args) {Receiver r = new Receiver();Command command = new ConcreteCommand(r);Invoke i = new Invoke(command);i.call();}}