public abstract class LampState {
String status;
abstract LampState click();
void showStatus() {
System.out.println("Lamp state is: " + status);
}
}
public class LampDim extends LampState {
LampDim() {
status = "Dim";
}
LampState click(){
return AllLampStates.BRIGHT;}
}
public interface AllLampStates {
public static final LampState OFF = new LampOff();
public static final LampState DIM = new LampDim();
public static final LampState BRIGHT = new LampBright();
}
public class Lamp {
LampState state;
Lamp() {
state = AllLampStates.OFF;
state.showStatus();
}
void click(){
state = state.click();
state.showStatus();
}
}
public class LampTest {
public static void main(String[] args) {
Lamp lamp = new Lamp();
lamp.click();
lamp.click();
lamp.click();
lamp.click();
lamp.click();
lamp.click();
}
}
public class LampBright extends LampState {
LampBright() {
status = "Bright";
}
LampState click(){
return AllLampStates.OFF;}
}
public class LampOff extends LampState {
LampOff() {
status = "Off";
}
LampState click(){
return AllLampStates.DIM;}
}