package command; import command.impl.NoCommand; /** * Created by http://teachcourse.cn on 2018/03/22. */ public class RemoteControl { Command[] onCommands; Command[] offCommands; Command undoCommand; public RemoteControl() { this.onCommands = new Command[7]; this.offCommands = new Command[7]; Command noCommand = new NoCommand(); for (int i = 0; i < 7; i++) { onCommands[i] = noCommand; offCommands[i] = noCommand; } undoCommand = noCommand; } public void setCommand(int slot, Command onCommand, Command offCommand) { onCommands[slot] = onCommand; offCommands[slot] = offCommand; } public void onButtonWasPushed(int slot) { onCommands[slot].execute(); undoCommand = onCommands[slot]; } public void offButtonWasPushed(int slot) { offCommands[slot].execute(); undoCommand = offCommands[slot]; } public void undoButterWasPushed() { undoCommand.undo(); } @Override public String toString() { StringBuffer stringBuff = new StringBuffer(); stringBuff.append("\n------Remote Control ------\n"); for (int i = 0; i < onCommands.length; i++) { stringBuff.append("[slot " + i + "]" + onCommands[i].getClass().getName() + " " + offCommands[i].getClass().getName() + "\n"); } return stringBuff.toString(); } }