需要支持事务操作。
public abstract class Command { //执行具体操作命令 public abstract void execute();}public class ConcreteCommand1 extends Command { PRivate Receiver receiver; public ConcreteCommand1() { this.receiver = new ConcreteReceiver1(); } public void setReceiver(Receiver receiver) { this.receiver = receiver; } @Override public void execute() { if(receiver!=null){ receiver.doSomething(); } }}public class ConcreteCommand2 extends Command { private Receiver receiver; public ConcreteCommand2() { this.receiver = new ConcreteReceiver2(); } public void setReceiver(Receiver receiver) { this.receiver = receiver; } @Override public void execute() { if(receiver!=null){ receiver.doSomething(); } }}public abstract class Receiver { public abstract void doSomething();}public class ConcreteReceiver1 extends Receiver { @Override public void doSomething() { System.out.println("ConcreteReceiver1 do..."); }}public class ConcreteReceiver2 extends Receiver { @Override public void doSomething() { System.out.println("ConcreteReceiver2 do..."); }}public class Invoker { private Command command; public void setCommand(Command command) { this.command = command; } public void action() { this.command.execute(); }}public class Test { public static void main(String args[]) { Invoker invoker = new Invoker(); Receiver receiver = new ConcreteReceiver1(); Command command = new ConcreteCommand1(); invoker.setCommand(command); invoker.action(); Receiver receiver2 = new ConcreteReceiver2(); Command command2 = new ConcreteCommand2(); invoker.setCommand(command2); invoker.action(); }}
新闻热点
疑难解答