class Test{
public void main(String[] args){
Context context = new Context();
context.handle();
context.handle();
}
}
abstract class State {
public abstract void handle(Context context);
}
class Context {
private State state;
public Context() {this.state = new Begin();}
public void setState(State state) {this.state = state;}
public State getState() {return (state);}
public void handle() {state.handle(this);}
}
class Begin extends State {
public void handle(Context context) {
System.out.println("当前状态是 Begin.");
context.setState(new Finish());
}
}
class Finish extends State {
public void handle(Context context) {
System.out.println("当前状态是 Finish.");
context.setState(new Begin());
}
}