public abstract class DogState {
String status;
abstract DogState command();
void showStatus() {
System.out.println("Dog is: " + status);
}
}
public class DogSit extends DogState {
DogSit() {
status = "Sitting";
}
DogState command(){
return AllDogStates.STAND;
}
}
public interface AllDogStates {
public static final DogState LIE = new DogLie();
public static final DogState SIT = new DogSit();
public static final DogState STAND = new DogStand();
}
public class Dog {
DogState state;
Dog() {
state = AllDogStates.LIE;
state.showStatus();
}
void command(){
state = state.command();
state.showStatus();
}
}
public class DogTest {
public static void main(String[] args) {
Dog dog = new Dog();
dog.command();
dog.command();
dog.command();
dog.command();
dog.command();
dog.command();
}
}
public class DogLie extends DogState {
DogLie() {
status = "Lying down";
}
DogState command(){
return AllDogStates.SIT;
}
}
public class DogStand extends DogState {
DogStand() {
status = "Standing";
}
DogState command(){
return AllDogStates.LIE;
}
}