on / off λ²νΌλ§ μλ 리λͺ¨μ½μ΄ μ‘΄μ¬ νλ€κ³ κ°μ
μ΄ λ¦¬λͺ¨μ½μλ€κ°, κ±°μ€μ νκ΄λ±μ ν€κ±°λ / κ±°μ€μ νκ΄λ±μ λκ³ , μ νκΈ°λ₯Ό μΌκ±°λ λλλ±μ μμ μ νλ€κ³ κ°μ
μ΄λ₯Ό ꡬννκΈ° μν΄, 리λͺ¨μ»¨ λ²νΌμ on / off λ₯Ό λλ μ λ, λμνλ λμμ ꡬ체μ μΈ λ΄μ©μ 리λͺ¨μ»¨μ λ΄μΌλ©΄, μ μ§λ³΄μκ° λ§€μ° μ΄λ €μ μ§
λ°λΌμ, 리λͺ¨μ»¨ λ²νΌμ on / off λ²νΌμλ€κ° κ° λ²νΌμ ꡬ체μ μΈ λ΄μ©μ μ€νν μ μλλ‘ κ΅¬ν ν΄λμ 컀맨λ κ°μ²΄ λ₯Ό μ¬μ΄λκ³ , λ²νΌμ λλ μ λ μ»€λ§¨νΈ κ°μ²΄μ λ΄κΈ΄ λ΄μ©μ μ€ννλλ‘ κ΅¬ν
μ’ λ νμ΄μ€λͺ
νλ©΄, 컀맨λ κ°μ²΄λ Command μΈν°νμ΄μ€μ ꡬν체
interface Command {
void execute()
}
interface Light {
void on();
void off();
}
class LightOnCommand implements Command {
Light light;
LightOnCommand(Light light) {
this.light = light;
}
void execute() {
light.on();
}
}
리λͺ¨μ»¨μ λ²νΌμλ commandλ₯Ό μ€μ
class RemoteControl {
Command[] onCommands;
Command[] offCommands;
public RemoteControl() {
onCommands = new Command[7]; // λ²νΌμ΄ 7κ°
offCommands = new Command[7]; // λ²νΌμ΄ 7κ°
Command noCommand = new NoCommand(); // Default command, nullμ μν
for (int i = 0; i < 7; i++) {
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
}
void setCommand(int slot, Command onCommand, Command offCommand) {
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
void onButtonPushed(int slot) {
onCommands[slot].execute();
}
void offButtonPushed(int slot) {
offCommands[slot].execute();
}
}
Undo κΈ°λ₯ μΆκ°
interface Command {
void execute()
void undo()
}
class LightOnCommand implements Command {
Light light;
LightOnCommand(Light light) {
this.light = light;
}
void execute() {
light.on();
}
void undo() {
light.off();
}
}
class RemoteControl {
Command[] onCommands;
Command[] offCommands;
Command undoCommand;
public RemoteControl() {
onCommands = new Command[7]; // λ²νΌμ΄ 7κ°
offCommands = new Command[7]; // λ²νΌμ΄ 7κ°
Command noCommand = new NoCommand(); // Default command, nullμ μν
for (int i = 0; i < 7; i++) {
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
undoCommand = noCommand;
}
void setCommand(int slot, Command onCommand, Command offCommand) {
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
void onButtonPushed(int slot) {
onCommands[slot].execute();
undoCommand = onCommands[slot];
}
void offButtonPushed(int slot) {
offCommands[slot].execute();
undoCommand = offCommands[slot];
}
void undoButtonPushed() {
undoCommand.undo();
}
}
리λͺ¨μ»¨μ Custom command μ λ ₯ κ°λ₯ ( Party Mode )
class MacroCommand implements Command {
Command[] commands;
MacroCommand(Command[] commands) {
this.commands = commands;
}
void execute() {
for (int i = 0; i < commands.length; i++) {
commands[i].execute();
}
}
}
Command[] partyOn = { lightOn, stereoOn ... }
Command[] partyOff = { lightOff, stereoOff ... }
MacroCommand partyOnMacro = new MacroCommand(partyOn);
MacroCommand partyOffMacro = new MacroCommand(partyOff);
remoteControl.setCommand(0, partyOnMacro, partyOffMacro);
컀맨λ ν¨ν΄μ νμ©νμ¬, Work Queue / μ€ν νμ€ν 리 μ μ₯ λ±μ ꡬν ν μ μμ
<aside> π‘ μ€νλ κΈ°λ₯μ μΊ‘μν ( Command μΈν°νμ΄μ€μ κ°μ²΄ν ) νμ¬, μ¬λ¬κ°μ μ€νλ κΈ°λ₯ ( μ¬λ¬ Command κ°μ²΄λ€ )μ λ΄μλκ³ μ¬μ©ν μ μλ ν΄λμ€ ( invoker )λ₯Ό λ§λ€μ΄ μ¬μ©νλ ν¨ν΄
</aside>