源自
State模式
允許一個對象在其狀態改變時
例子
view plaincopy to clipboardprint?
public interface State {
public void handle(Context ctx);
}
public class Context {
private State _state;
public Context(State state) {
_state = state;
}
public void request() {
if (_state != null) {
_state
}
}
public void ChangeState(State s) {
if (_state != null) {
_state = null;
}
_state = s;
}
}
public class ConcreteStateA implements State {
public void handle(Context ctx) {
System
if (ctx != null) {
ctx
}
}
}
public class ConcreteStateB implements State {
public void handle(Context ctx) {
System
if (ctx != null) {
ctx
}
}
}
public class StateClient {
public static void main(String[] args) {
State state = new ConcreteStateA();
Context context = new Context(state);
context
context
context
context
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25974.html