001package headfirst.command.remote; 002 003// 004// This is the invoker 005// 006public class RemoteControl { 007 Command[] onCommands; 008 Command[] offCommands; 009 010 public RemoteControl() { 011 onCommands = new Command[7]; 012 offCommands = new Command[7]; 013 014 Command noCommand = new NoCommand(); 015 for (int i = 0; i < 7; i++) { 016 onCommands[i] = noCommand; 017 offCommands[i] = noCommand; 018 } 019 } 020 021 public void setCommand(int slot, Command onCommand, Command offCommand) { 022 onCommands[slot] = onCommand; 023 offCommands[slot] = offCommand; 024 } 025 026 public void onButtonWasPushed(int slot) { 027 onCommands[slot].execute(); 028 } 029 030 public void offButtonWasPushed(int slot) { 031 offCommands[slot].execute(); 032 } 033 034 public String toString() { 035 StringBuilder stringBuff = new StringBuilder(); 036 stringBuff.append("\n------ Remote Control -------\n"); 037 for (int i = 0; i < onCommands.length; i++) { 038 stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName() 039 + " " + offCommands[i].getClass().getName() + "\n"); 040 } 041 return stringBuff.toString(); 042 } 043}