/**
* 上下文环境(Context)角色
*/
public class Context {
private State state;
public Context() {
state = null;
}
public void setState(State state) {
this.state = state;
}
public State getState() {
return state;
}
}
/**
* 抽象状态(State)角色
*/
public interface State {
public void doAction(Context context);
}
/**
* 具体状态(Concrete State)
*/
public class StartState implements State {
public void doAction(Context context) {
System.out.println("Player is in start state.");
context.setState(this);
}
public String toString() {
return "Start State";
}
}
/**
* 具体状态(Concrete State)
*/
public class StopState implements State {
public void doAction(Context context) {
System.out.println("Player is in stop state.");
context.setState(this);
}
public String toString() {
return "Stop State";
}
}
/**
* 客户端
*/
public class Client {
public static void main(String[] args) {
Context context = new Context();
StartState startState = new StartState();
startState.doAction(context);
System.out.println(context.getState().toString());
StopState stopState = new StopState();
stopState.doAction(context);
System.out.println(context.getState().toString());
}
}