728x90
public interface Command {
public void execute();
//public void undo();
}
public class NullCommand implements Command{
@Override
public void execute() {
}
}
/*버튼 하나로 여러 개를 켜고 싶을 때*/
public class enterCommand implements Command {
Light myLight;
TV myTv;
enterCommand(Light light, TV tv){
this.myLight = light;
this.myTv = tv;
}
@Override
public void execute() {
myLight.on();
myTv.on();
}
}
public class LightOnCommand implements Command{
Light light;
public LightOnCommand(Light light){this.light=light;}
public void execute(){light.on();}
}
public class LightOffCommand implements Command{
Light light;
public LightOffCommand(Light light){this.light=light;}
public void execute(){light.off();}
}
public class TVOnCommand implements Command{
TV tv;
public TVOnCommand(TV tv){this.tv=tv;}
public void execute(){tv.on();}
}
public class TVOffCommand implements Command{
TV tv;
public TVOffCommand(TV tv){this.tv=tv;}
public void execute(){tv.off();}
}
public class Light {
String location = "";
public Light(String location) {
this.location = location;
}
public void on() {
System.out.println(location + " light is on");
}
public void off() {
System.out.println(location + " light is off");
}
}
public class TV {
String location;
int channel;
public TV(String location) {
this.location = location;
}
public void on() {
System.out.println("TV is on");
}
public void off() {
System.out.println("TV is off");
}
public void setInputChannel() {
this.channel = 3;
System.out.println("Channel is set for VCR");
}
}
public class RemoteControl {
Command[] onCommand = new Command[7];
Command[] offCommand = new Command[7];
Command undoCommand = new NullCommand();
RemoteControl(){
for(int i=0;i<7;i++){
onCommand[i]=new NullCommand();
offCommand[i]=new NullCommand();
}
}
public void setOnCommand(int num, Command myCommand){
onCommand[num] = myCommand;
}
public void setOffCommand(int num, Command myCommand){
offCommand[num] = myCommand;
}
public void onButtonWasPushed(int num){
onCommand[num].execute();
}
public void offButtonWasPushed(int num){
offCommand[num].execute();
}
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();
}
}
public class Main {
public static void main(String[] args){
Light livingRoomLight = new Light("Living Room");
Light bedRoomLight = new Light("Bed Room");
TV myTV= new TV("Living Room");
RemoteControl myRemote = new RemoteControl();
myRemote.setOnCommand(0, new LightOnCommand(livingRoomLight));
myRemote.setOffCommand(0, new LightOffCommand(livingRoomLight));
myRemote.setOnCommand(1,new TVOnCommand(myTV));
myRemote.setOffCommand(1, new TVOffCommand(myTV));
myRemote.setOnCommand(2, new LightOnCommand(bedRoomLight));
myRemote.setOffCommand(2, new LightOffCommand(bedRoomLight));
myRemote.setOnCommand(5, new enterCommand(livingRoomLight, myTV));
myRemote.onButtonWasPushed(0);
myRemote.onButtonWasPushed(1);
myRemote.offButtonWasPushed(0);
myRemote.onButtonWasPushed(2);
myRemote.onButtonWasPushed(5);
}
}
728x90
'기타 > Java' 카테고리의 다른 글
[디자인패턴]Iterator Pattern (0) | 2021.12.02 |
---|---|
[Design Pattern]Adapter Pattern (0) | 2021.11.16 |
[Java]Design Pattern-Singleton (0) | 2021.11.03 |
[Java]Design Pattern-Decorator Pattern (0) | 2021.10.28 |
[Java]Design Pattern-Simple Factory (0) | 2021.10.28 |