2.1 문제점
2.2 해결책
3.1 Command 인터페이스 만들기
public interface Command{
public void execute();
}
3.2 전등을 켜기 위한 커맨드 클래스 구현
public class LightOnCommand implements Command{
Light light;
public LightOnCommand(Light light){
this.light = light;
}
public void execute(){
light.on();
}
}
3.3 커맨드 객체 사용하기
public class SimpleRemoteControl{
Command slot;
public SimpleRemoteControl(){}
public void setCommand(Command command){
slot = command;
}
public void buttonWasPressed(){
slot.execute();
}
}
3.4 리모컨을 사용하기 위한 간단한 테스트 클래스
public class RemoteControlTest{
public static void main(String[] agrs){
SimpleRemoteControlremote remote = new SimpleRemoteControl();
Light light = new Light();
LightOnCommand lightOn = new LightOnCommand(light);
remote.setCommand(lightOn);
remote.butonWasPressed();
}
}
public class RemoteControl{
Command[] onCommands;
Command[] offCommands;
public RemoteControl(){
onCommands = new Commnad[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 offCommnad){
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
public void onButtonWasPushed(int slot){
onCommands[slot].execute();
}
public void offButtonWasPushed(int slot){
offCommands[slot].execute();
}
public String toString(){
StringBuffer stringBuff = new StringBuffer();
stringBuff.append("\n-------Remote Control ------\n");
for(int i=0l i<onCommands.lentgh; i++){
stringBuffer.append("[slot " + i + "]" + onCommands[i].getClass().getName()
+ " " + offCommands[i].getClass().getName() + "\n");
}
return stringBuff.toString();
}
}
public class StereoOnWithCDCommand implements Command{
Stereo stereo;
public StereoOnWithCDCommand(Stereo stereo){
this.stereo = stereo;
}
public void execute(){
stereo.on();
stereo.setCD();
stereo.setVolume(11);
}
}
6.1 Command 인터페이스 만들기
public interface Command{
public void execute();
public void undo();
}
6.2 전등을 켜기 위한 커맨드 클래스 구현
public class LightOnCommand implements Command{
Light light;
public LightOnCommand(Light light){
this.light = light;
}
public void execute(){
light.on();
}
public void undo(){
light.off();
}
}
6.3 전등을 끄기 위한 커맨드 클래스 구현
public class LightOffCommand implements Command{
Light light;
public LightOffCommand(Light light){
this.light = light;
}
public void execute(){
light.off();
}
public void undo(){
light.on();
}
}
6.4 리모컨 undo
public class RemoteControl{
Command[] onCommands;
Command[] offCommands;
Command undoCommand;
public RemoteControl(){
onCommands = new COmmnad[7];
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 offCommnad){
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
public void onButtonWasPushed(int slot){
onCommands[slot].execute();
undoCommand = onCommand[slot];
}
public void offButtonWasPushed(int slot){
offCommands[slot].execute();
undoCommand = offCommand[slot];
}
public void undoButtonWasPushed(){
undoCommand.undo();
}
public String toString(){
StringBuffer stringBuff =new StringBuffer();
stringBuff.append("\n-------Remote Control ------\n");
for(int i=0l i<onCommands.lentgh; i++){
stringBuffer.append("[slot " + i + "]" + onCommands[i].getClass().getName()
+ " " + + offCommands[i].getClass().getName() + "\n");
}
return stringBuff.toString();
}
}
버튼 한 개만 누루면 전등이 어두워지면서 오디오, TV가 켜지고, DVD모드로 변경되고, 욕조에 물이 채워지는것까지 한번에 켜지는 기능?
public class MacroCommand impelnets Command{
Commnad[] commands;
public MacroCommand(Commnad[] commands){
this.commands = commands;
}
public void execute(){
for(int i=0; i<commdands.lentth; i++){
commands[i].execute();
}
}
}
public class party{
Light light = new Light("living Room");
TV tv = new TV("Living Room");
Stereo stereo = new stereo("Living Room");
Hottbu hottub = new Hottub();
LightOnCommand lightOn = new LightOnCOmmand(light);
StereoOnCommand stereoOn = new StereoOnCommand(stereo);
TVOnCommand tvOn = new VTOnCommnad(tv);
HottubOnCommand hottubOn = new HottubOnCommand(hottub);
Command[] partyOn = {lightOn, stereoOn, tvOn, hottubOn};
Commnad[] partyOff = {lightOff, stereoOff, tvOff, hottubOff};
MacroCommand partyOnMacro = new MacroCommand(partyOn);
MacroCommand partyOffMacro = new MacroCommand(partyOff);
remotoCOntrol.setCommand(0, partyOnMarco, partyOffMacro);
}