状态模式
2020-05-20 本文已影响0人
simplerandom
public interface State {
void shouldDo(String s);
}
public class CloseState implements State {
@Override
public void shouldDo(String s) {
System.out.println("关闭状态:" + s);
}
}
public class OpenState implements State {
@Override
public void shouldDo(String s) {
System.out.println("开放状态:" + s);
}
}
public class AccordingStateDo {
State state;
public AccordingStateDo(State state) {
this.state = state;
}
void defaultDo(String s){
state.shouldDo(s);
}
}
测试
public class Test {
public static void main(String[] args) {
AccordingStateDo accordingStateDo = new AccordingStateDo(new CloseState());
accordingStateDo.defaultDo("停止");
AccordingStateDo accordingStateDo1 = new AccordingStateDo(new OpenState());
accordingStateDo1.defaultDo("继续");
}
}