import java.util.LinkedList;
enum State
{
STATE1,
STATE2,
STATE3,
STATE4
}
enum Input{
INPUT1,
INPUT2,
INPUT3,
INPUT4
}
class Record {
public State currentState;
public Input input;
public State nextState;
public Record(State current_state, Input input, State next_state) {
this.currentState = current_state;
this.input = input;
this.nextState = next_state;
}
}
public class FSM
{
public LinkedList<Record> fsms;
State CurrentState;
FSM()
{
fsms = new LinkedList<>();
fsms.add(new Record(State.STATE1, Input.INPUT1, State.STATE2));
fsms.add(new Record(State.STATE2, Input.INPUT2, State.STATE3));
fsms.add(new Record(State.STATE3, Input.INPUT3, State.STATE4));
CurrentState = State.STATE1;
}
public void computeNextState(Input input)
{
for (Record record: fsms)
{
if (CurrentState == record.currentState && record.input == input)
{
CurrentState = record.nextState;
return;
}
}
}
public static void main(String[] args) {
FSM fsm = new FSM();
System.out.println(fsm.CurrentState);
fsm.computeNextState(Input.INPUT1);
System.out.println(fsm.CurrentState);
}
}
在销售的时候,比如:
1、开始初始状态;
2、当前如果是初始状态,如果用户下单,当前状态就会转化为待付款状态;
3、当前如果是待付款状态,如果用户完成付款,当前状态就会转化为待发货状态;
4、当前如果是待付款状态,如果用户取消订单,当前状态就会转化为初始状态;
5、当前如果是待发货状态,如果仓库完成发货,当前状态就会转化为已发货状态;
当前状态 | 输入 | 下一个状态 |
---|---|---|
初始状态 | 用户下单 | 待付款 |
待付款 | 用户完成付款 | 待发货状态 |
待付款 | 用户取消付款 | 初始状态 |
待发货 | 仓库完成发货 | 已发货状态 |
import java.util.LinkedList;
enum State2
{
ChuShi,
DaiFukuan,
DaiFaHuo,
YiFaHuo
}
enum Input2{
YongHuXiaDan,
YongHuWanChengFuKuan,
YongHuQuXiaoFuKuan,
CangKuWanChengFaHuo
}
class Record2 {
public State2 currentState;
public Input2 input;
public State2 nextState;
public Record2(State2 currentState, Input2 input, State2 nextState) {
this.currentState = currentState;
this.input = input;
this.nextState = nextState;
}
}
public class FSM2
{
public LinkedList<Record2> fsms;
State2 CurrentState;
FSM2()
{
fsms = new LinkedList<>();
fsms.add(new Record2(State2.ChuShi, Input2.YongHuXiaDan, State2.DaiFukuan));
fsms.add(new Record2(State2.DaiFukuan, Input2.YongHuWanChengFuKuan, State2.DaiFaHuo));
fsms.add(new Record2(State2.DaiFukuan, Input2.YongHuQuXiaoFuKuan, State2.ChuShi));
fsms.add(new Record2(State2.DaiFaHuo, Input2.CangKuWanChengFaHuo, State2.YiFaHuo));
CurrentState = State2.ChuShi;
}
public void computeNextState(Input2 input)
{
for (Record2 record: fsms)
{
if (CurrentState == record.currentState && record.input == input)
{
System.out.println("当前状态: " + CurrentState + " 用户完成操作:" + input + " 新的状态: " + record.nextState);
CurrentState = record.nextState;
return;
}
}
}
public void resetCurrentState()
{
CurrentState = State2.ChuShi;
}
public static void main(String[] args) {
FSM2 fsm = new FSM2();
fsm.computeNextState(Input2.YongHuXiaDan);
fsm.computeNextState(Input2.YongHuWanChengFuKuan);
fsm.computeNextState(Input2.CangKuWanChengFaHuo);
System.out.println();
fsm.resetCurrentState();
fsm.computeNextState(Input2.YongHuXiaDan);
fsm.computeNextState(Input2.YongHuQuXiaoFuKuan);
}
}
运行效果:
实际上,状态机在网络底层开发用的比较多,另外,比如在项目菜单开的状态也可以使用,比如主页面,根据不同的用户操作进入不同的子页面(不同的状态),再根据用户的不同输入,再进入下一个子页面,这样就形成了一系统状态转换表,这个例子是比较简单的,实际开发中的比较复杂,当然,说的不一定对。