
you are here
221
6
章
Command
パターン
public class CeilingFan {
public static final int HIGH = 3;
public static final int MEDIUM = 2;
public static final int LOW = 1;
public static final int OFF = 0;
String location;
int speed;
public CeilingFan(String location) {
this.location = location;
speed = OFF;
}
public void high() {
speed = HIGH;
//
ファンの強さを「強」に設定するコード
}
public void medium() {
speed = MEDIUM;
//
ファンの強さを「中」に設定するコード
}
public void low() {
speed = LOW;
//
ファンの強さを「弱」に設定するコード
}
public void off() {
speed = OFF;
//
ファンを止めるコード
}
public int getSpeed() {
return speed;
}
}
状態を使ってアンドゥを実装する
Light
に対するアンドゥの実装は勉強にはなりましたが、少し簡単すぎました。通常、
アンドゥを実装するには状態を管理する必要があります。ベンダークラスの ...