public interface Command {
void exe();
}
public class Light {
public void on(){
System.out.println("打開");
}
}
public class LightCommand implements Command {
private Light light;
public LightCommand(Light l) {
this.light = l;
}
@Override
public void exe() {
light.on();
}
}
public class RemoteControl {
private Command command;
public void setCommand(Command c) {
this.command = c;
}
public void pressButton() {
command.exe();
}
}
public abstract class Main {
public static void main(String[] args) {
Light light = new Light();
Command command = new LightCommand(light);
RemoteControl remoteControl = new RemoteControl();
remoteControl.setCommand(command);
remoteControl.pressButton();
}
}
輸出:
打開