

즉 종업원과 주방장은 주문서로 통신


public interface Command {
public void excute();
}
public class LightOnCommand implements Command {
Light light;
public LightOnCommand(Light light) {
this.light = light;
}
public void excute() {
light.on();
}
}
public class SimpleRemoteControl {
Command slot;
public SimpleRemoteContol() {}
public void setCommand(Command command) {
slot = command;
}
public void buttonWasPressed() {
slot.execute();
}
}
public class RemoteContolTest {
public static void main(String[] args) {
SimpleRemoteContol remote = new SmipleRemoteControl();
Light light = new Light();
LightOnCommand lightOn = new LightOnCommand(light);
remote.setCommand(lightOn);
remote.buttonWasPressed();
}
}
커맨드 패턴을 사용하면 요청 내역을 객체로 캡슐화 해서 객체를 서로 다른 요청 내역에 따라 매개변수 화 할 수 있음. 이러면 요청을 큐에 저장하거나 로그로 기록하거나 작업 취소 기능을 사용가능함
커맨드 객체는 일련의 행동을 특정 리시버와 연결함으로 요청을 캡슐화 한것
행동과 리시버를 한 객체에 넣고 , excute()라는 메소드 하나만 외부에 공개하는 방법

public class RemoteControl {
Command[] onCommands;
Command[] offCommands;
public RemoteControl() {
onCommands = new Command[7];
offCommands = new Command[7];
Command noCommand = new NoCommand();
for(int i =0; i< 7; i++){
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
}
public void setCommand(int slot, Command onCommand, Command offCommand) {
onCommands[slot] = onCommand;
offCommand[slot] = offCommand;
}
public void onButtonWasPushed(int slot) {
onCommands[slot].excute();
}
public void offButtonWasPushed(int slot) {
offCommands[slot].excute();
}
pbulic String toString() {
StringBuffer stringBuff = new StringBuffer();
StringBuff.append("리모컨");
for (int i = 0; i < onCommands.length; i++) {
stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName() + " " + offCommands[i].getClass().getNmae();
}
return stringBuff.toString();
}
}
코드를public class LightOffCommand implements Command {
Light light;
public LightOnCommand(Light light) {
this.light = light;
}
public void excute() {
light.off();
}
} 입력하세요
public class RemoteLeader {
public static void main(String[] args) {
RemoteControl remoteControl = new RemoteControl();
Light livingRoomLight = new Light("Living Room");
Light kitchenLight = new Light("Kitchen");
CeilingFan ceilingFan = new CeilingFan("Living Room");
GarageDoor garageDoor = new GarageDoor("Garage");
Stereo stereo = new Stereo("Living Room");
LightOnCommand livingRoomLightOn =
new LightOnCommand(livingRoomLight);
LightOffCommand livingRoomLightOff =
new LightOffCommand(livingRoomLight);
LightOnCommand kitchenLightOn =
new LightOnCommand(kitchenLight);
LightOffCommand kitchenLightOff =
new LightOffCommand(kitchenLight);
CeilingFanOnCommand ceilingFanOn =
new CeilingFanOnCommand(ceilingFan);
CeilingFanOffCommand ceilingFanOff =
new CeilingFanOffCommand(ceilingFan);
GarageDoorUpCommand garageDoorUp =
new GarageDoorUpCommand(garageDoor);
GarageDoorDownCommand garageDoorDown =
new GarageDoorDownCommand(garageDoor);
StereoOnWithCDCommand stereoOnWithCD =
new StereoOnWithCDCommand(stereo);
Stereo

NoCommand객체는 일종의 널 객체 널 객체는 딱히 리턴할 것도 없고 클라이언트가 null을 처리하지 않게 하고 싶을때 활용하면 좋습니다. 널 객체는 여러 디자인 패턴에서 유용하게 쓰임 널객체를 일종의 디자인 패턴이라고도 부름