标签:context public pre default code class sys 设计模式 action
模式:行为型模式
? 代码
上下文
public interface Context {
public void setState(State state);
public void print();
}
上下文实现
public class DefaultContext implements Context {
private State state;
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
@Override
public void print() {
this.state.print();
}
}
状态抽象
public interface State {
public String doAction(Context context);
public void print();
}
状态实现1
public class StartState implements State {
@Override
public String doAction(Context context) {
context.setState(this);
return null;
}
@Override
public void print() {
System.out.println("StartState");
}
}
状态实现2
public class EndState implements State {
@Override
public String doAction(Context context) {
context.setState(this);
return null;
}
public void print(){
System.out.println("EndState");
}
}
标签:context public pre default code class sys 设计模式 action
原文地址:https://www.cnblogs.com/tjc1996/p/10815599.html