public class Executor {
public void execOne(){
System.out.println("烤羊肉串儿");
}
public void execTwo(){
System.out.println("烤鸡翅膀儿");
}
}
public abstract class Command {
protected Executor executor;
public Command(Executor executor) {
this.executor = executor;
}
public abstract void exec();
}
public class CommandOne extends Command {
public CommandOne(Executor executor) {
super(executor);
}
@Override
public void exec() {
executor.execOne();
}
}
public class CommandTwo extends Command {
public CommandTwo(Executor executor) {
super(executor);
}
@Override
public void exec() {
executor.execTwo();
}
}
public class Dispatcher {
// private Command command;
// public void setCommand(Command command) {
// this.command = command;
// }
// public void callExec() {
// command.exec();
// }
private List<Command> commandList = new ArrayList<>();
public void setCommand(Command command) {
commandList.add(command);
}
public void cancelExec(Command command) {
commandList.remove(command);
}
public void callExec() {
for (Command command : commandList) {
command.exec();
}
}
}
public class RunMain {
public static void main(String[] args) {
Executor executor = new Executor();
Command commandOne = new CommandOne(executor);
Command commandTwo = new CommandTwo(executor);
Dispatcher dispatcher = new Dispatcher();
dispatcher.setCommand(commandOne);
dispatcher.setCommand(commandTwo);
dispatcher.callExec();
}
}