模型图
涉及的角色及说明
何为有限状态机
有限状态机在维基百科中的解释是:
有限状态机(英语:finite-state machine,缩写:FSM)又称有限状态自动机,简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型。
咋一看好像很虚幻,我们先看一下地铁运营的例子:
站在有限状态机的角度来看,可以抽象如下几个关键点:
状态(State)
即地铁所处的状态,同上述例子的:“行进中”,“到站-开门”,“到站-关门”。
事件(Event)
即地铁都是在触发了某个事件才往下更改状态的,如:“行进中”触发了“刹车”事件才变成“到站-关门”的。
动作(Transition)
即地铁流转过程中具体的业务逻辑,如:“到站-关门”触发“启动”事件变成“行进中”,这中间可能需要发送出站通知,播放广播等操作。
示例说明
地铁的状态流转
类图
状态
public class SubwayState {
/**
* 状态编码
*/
@Getter
private StateCodeEnums stateCode;
/**
* 当前状态下可允许执行的动作
*/
@Getter
private List<SubwayTransition> transitions = new ArrayList<>();
public SubwayState(StateCodeEnums stateCode, SubwayTransition... transitions) {
this.stateCode = stateCode;
for (SubwayTransition transition : transitions) {
this.transitions.add(transition);
}
}
// 添加动作
public void addTransition(SubwayTransition transition) {
transitions.add(transition);
}
@Override
public String toString() {
return stateCode.getDisplayName();
}
}
状态枚举类:
/**
* 类描述: 状态. <br />
*
* @author hanjun.hw
* @since 2018/9/29
*/
public enum StateCodeEnums implements IEnum {
/**
* 管理员
*/
MOVING("MOVING", "行进中"),
/**
* 普通用户
*/
CLOSED("CLOSED", "到站-关门"),
OPEN("OPEN", "到站-开门"),
SUSPENDED("SUSPENDED", "停运的");
private String code;
private String displayName;
StateCodeEnums(String code, String displayName) {
this.code = code;
this.displayName = displayName;
}
@Override
public String getCode() {
return code;
}
@Override
public String getDisplayName() {
return displayName;
}
@Override
public String toString() {
return displayName;
}
}
每个状态拥有不同的动作集合
状态-行进中:[刹车],
状态-关门:[开门,启动],
状态-开门:[关门],
状态-停运:[启动],
这个关联关系是在哪里定义的呢?
在状态机初始化时创建
说明:
状态A 有三种动作,每个动作都是由某个具体事件触发,
一个事件只能触发一个动作
事件
public class SubwayEvent {
/**
* 事件标识(编码)
*/
@Getter
private EventCodeEnums eventCode;
/**
* 附属的业务参数
*/
@Getter
@Setter
private Map<Object, Object> attributes = null;
public SubwayEvent(EventCodeEnums eventCode) {
this.eventCode = eventCode;
}
public SubwayEvent(EventCodeEnums eventCode, Map<Object, Object> attributes) {
this.eventCode = eventCode;
this.attributes = attributes;
}
@Override
public String toString() {
return eventCode.getCode();
}
}
事件枚举
/**
* 类描述: 事件类型. <br />
*
* @author hanjun.hw
* @since 2018/9/29
*/
public enum EventCodeEnums implements IEnum {
/**
* 管理员
*/
START_UP("START_UP", "启动"),
/**
* 普通用户
*/
CLOSING("CLOSING", "关门"),
OPENING("OPENING", "开门"),
BRAKING("BRAKING", "刹车");
private String code;
private String displayName;
EventCodeEnums(String code, String displayName) {
this.code = code;
this.displayName = displayName;
}
@Override
public String getCode() {
return code;
}
@Override
public String getDisplayName() {
return displayName;
}
@Override
public String toString() {
return displayName;
}
}
事件和动作的关系
事件和动作是一一对应的。
每个事件会触发特定的动作
动作
public abstract class SubwayTransition {
/**
* 触发事件
*/
@Getter
private EventCodeEnums eventCode;
/**
* 触发当前状态
*/
@Getter
private SubwayState currState;
/**
* 触发后状态
*/
@Getter
private SubwayState nextState;
public SubwayTransition(EventCodeEnums eventCode, SubwayState currState, SubwayState nextState) {
super();
this.eventCode = eventCode;
this.currState = currState;
this.nextState = nextState;
}
/**
* 执行动作
*
* @param event
* @return
* @author 张振伟
*/
public SubwayState execute(SubwayEvent event) {
System.out.println(String.format("当前是:%s 状态,执行:%s 操作后,流转成:\"%s\" 状态。", currState, eventCode, nextState));
if (this.doExecute(event)) {
return this.nextState;
} else {
return null;
}
}
/**
* 执行动作的具体业务
*
* @param event
* @return
* @author 张振伟
*/
protected abstract boolean doExecute(SubwayEvent event);
}
状态机
抽象类
public abstract class SubwayAbsStateMachine {
/**
* 定义的所有状态
*/
private static List<SubwayState> allStates = null;
/**
* 状态机执行事件
*
* @param stateCode
* @param event
* @return
*/
public SubwayState execute(StateCodeEnums stateCode, SubwayEvent event) {
SubwayState startState = this.getState(stateCode);
for (SubwayTransition transition : startState.getTransitions()) {
if (event.getEventCode().equals(transition.getEventCode())) {
return transition.execute(event);
}
}
log.error("StateMachine[{}] Can not find transition for stateId[{}] eventCode[{}]", this.getClass().getSimpleName(), stateCode, event.getEventCode());
System.out.println(String.format("StateMachine[%s] Can not find transition for current state:[%s] eventCode:[%s]", this.getClass().getSimpleName(), stateCode, event.getEventCode()));
return null;
}
public SubwayState getState(StateCodeEnums stateCode) {
if (allStates == null) {
log.info("StateMachine declareAllStates");
allStates = this.declareAllStates();
}
for (SubwayState state : allStates) {
if (state.getStateCode().equals(stateCode)) {
return state;
}
}
return null;
}
/**
* 由具体的状态机定义所有状态
*
* @return
* @author 张振伟
*/
public abstract List<SubwayState> declareAllStates();
}
实现类
public class SubwayStateMachine extends SubwayAbsStateMachine {
@Override
public List<SubwayState> declareAllStates() { // 定义状态机的状态
List<SubwayState> stateList = new ArrayList<>();
SubwayState movingState = new SubwayState(StateCodeEnums.MOVING);
SubwayState closedState = new SubwayState(StateCodeEnums.CLOSED);
SubwayState openState = new SubwayState(StateCodeEnums.OPEN);
SubwayState suspensionState = new SubwayState(StateCodeEnums.SUSPENDED);
movingState.addTransition(new BrakeTransition(movingState, closedState));
closedState.addTransition(new StartupTransition(closedState, movingState));
closedState.addTransition(new OpenTransition(closedState, openState));
openState.addTransition(new CloseTransition(openState, closedState));
suspensionState.addTransition(new StartupTransition(suspensionState, movingState));
stateList.add(movingState);
stateList.add(closedState);
stateList.add(openState);
stateList.add(suspensionState);
return stateList;
}
/**
* 定义“刹车”动作
*/
public class BrakeTransition extends SubwayTransition {
public BrakeTransition(SubwayState currState, SubwayState nextState) {
super(EventCodeEnums.BRAKING, currState, nextState);
}
@Override
protected boolean doExecute(SubwayEvent event) {
System.out.println("执行刹车操作...");
return true;
}
}
/**
* 定义“启动”动作
*/
public class StartupTransition extends SubwayTransition {
public StartupTransition(SubwayState currState, SubwayState nextState) {
super(EventCodeEnums.START_UP, currState, nextState);
}
@Override
protected boolean doExecute(SubwayEvent event) {
System.out.println("执行启动操作...");
return true;
}
}
/**
* 关门
*/
public class CloseTransition extends SubwayTransition {
public CloseTransition(SubwayState currState, SubwayState nextState) {
super(EventCodeEnums.CLOSING, currState, nextState);
}
@Override
protected boolean doExecute(SubwayEvent event) {
System.out.println("执行关门操作...");
return true;
}
}
/**
* 开门
*/
public class OpenTransition extends SubwayTransition {
public OpenTransition(SubwayState currState, SubwayState nextState) {
super(EventCodeEnums.OPENING, currState, nextState);
}
@Override
protected boolean doExecute(SubwayEvent event) {
System.out.println("执行开门操作...");
return true;
}
}
}
状态机实现类做了哪些事情?
- 定义所有的状态(有限个状态);
- 每个状态的动作集合;
- 每个动作的定义:(a)由什么事件触发?(b)状态的流转(从源状态到目标状态)
测试类
public class TestSubwayStateMachine {
@Test
public void test() {
SubwayAbsStateMachine sm = new SubwayStateMachine();
SubwayState state = sm.execute(StateCodeEnums.MOVING, new SubwayEvent(EventCodeEnums.BRAKING));
}
}
思考&借鉴
- 状态机模式的前提是 有限个状态,不适用无线个状态的场景;
- 每个状态都有特定的动作集合;动作是由事件触发的;
- 可以把状态机模式和观察者模式进行比较,也是以事件驱动的,
SubwayTransition 可以看做监听程序,
每个事件都有注册事件监听程序
借鉴
什么场景适合使用状态机模式?
- 有静态的状态,并且是有限的;
- 业务逻辑围绕 不同状态之间的流转切换来实现;
- 状态之间的切换 往往通过不同的事件来触发(驱动)
举例:
a. 银行账户状态
正常,锁定,冻结
b. 电脑的状态
开启,待机,关机,锁屏
c. 房源的状态
看房中,验房中,待签约,已签约,待评价等