一 状态模式下有几个重要的内容
(1)状态 表示当前系统处于那种状态下
(2)事件 有哪些事件会导致状态的转变
(3)状态转变 当前状态下,发生了某个事件 这时候状态会转变成什么样
二 整体设计
(1)对于状态,首先定义上层状态State接口,且有一个方法process
然后定义一个具体的状态,针对每一个状态,会实现process,即该状态下会做什么动作。这里定义了四个状态
AgingState:老化状态----老化数据
public class AgingState implements State {
@Override
public void process() {
System.out.println("现在正在老化数据呢");
}
}
GrState:积压状态------给数据打标签
public class GrState implements State {
@Override
public void process() {
System.out.println("您现在正处于GR状态中,开始打标签了。");
}
}
Begin状态----表示服务启动状态
public class BeginState implements State {
@Override
public void process() {
System.out.println("正在处于开始状态,什么都不做");
}
}
Wating状态-----表示当前处于等待状态中
public class WatingState implements State {
@Override
public void process() {
System.out.println("正在等待老化定时器的结束");
}
}
(2)对于事件,定义Event接口
public interface Event {
}
具体的事件有:Down事件,Up事件,结束等待事件
(3)状态的转化:
首先定义一个状态转化的基础类StateTransfer
public class StateTransfer {
private State srcState;
private State dstState;
private Event event;
public StateTransfer(State srcState, State dstState, Event event) {
this.srcState = srcState;
this.dstState = dstState;
this.event = event;
}
}
(4)定义具体的状态管理容器
import java.util.ArrayList;
import java.util.List;
public class StateContext {
private static List<StateTransfer> stateList = new ArrayList<>();
public static void addState(StateTransfer state) {
stateList.add(state);
}
public static List<StateTransfer> getStateList() {
return stateList;
}
public static void init() {
State beginState = new BeginState();
State grState = new GrState();
State watingState = new WatingState();
State agingState = new AgingState();
StateContext.addState(new StateTransfer(beginState, grState, new DownEvent()));
StateContext.addState(new StateTransfer(grState, watingState, new UpEvent()));
StateContext.addState(new StateTransfer(watingState, agingState, new EndWaitEvent()));
StateContext.addState(new StateTransfer(grState, agingState, new EndWaitEvent()));
StateContext.addState(new StateTransfer(watingState, watingState, new UpEvent()));
}
}
(5)状态管理器
在状态管理器中,首先需要传入当前的状态进来,得到状态管理机,然后根据对应的发生的事件,状态会发生不同的转变,从而做出不同的动作。
public interface StateManager {
State getState(State state, Event event);
void doProcess(Event event);
}
具体实现:
public class StateManagerImpl implements StateManager {
private State state;
public StateManagerImpl(State state) {
this.state = state;
}
@Override
public State getState(State state, Event event) {
for (StateTransfer transferState : StateContext.getStateList()) {
if (transferState.getSrcState().getClass() == state.getClass()
&& transferState.getEvent().getClass() == event.getClass()) {
return transferState.getDstState();
}
}
return null;
}
@Override
public void doProcess(Event event) {
State nextState = getState(state, event);
if (nextState != null) {
this.state = nextState;
state.process();
}
}
}
(6)效果
public class Main {
public static void main(String[] args) {
StateContext.init();
StateManager stateManager = new StateManagerImpl(new BeginState());
stateManager.doProcess(new DownEvent());
stateManager.doProcess(new UpEvent());
stateManager.doProcess(new EndWaitEvent());
}
}
后台打印: