
204
6
章
コマンドオブジェクトを使用する
public class SimpleRemoteControl {
Command slot;
public SimpleRemoteControl() {}
public void setCommand(Command command) {
slot = command;
}
public void buttonWasPressed() {
slot.execute();
}
}
public class RemoteControlTest {
public static void main(String[] args) {
SimpleRemoteControl remote = new SimpleRemoteControl();
Light light = new Light();
LightOnCommand lightOn = new LightOnCommand(light);
remote.setCommand(lightOn);
remote.buttonWasPressed();
}
}
コマンドオブジェクトを使用する
単純化してみましょう。リモコンにはボタンが
1
つと、このボタンに対応して操
作できるデバイスのスロットがあるとします。
以下は、この簡単なリモコンをテストするちょっとしたコードです。このコードを
見てください。各部分がどのように
Command
パターン図に対応しているか説明します。
ファイル 編集 ウィンドウ ヘルプ 食堂の食べ物美味しい ...