Skip to content

Instantly share code, notes, and snippets.

@yicone
Created June 2, 2020 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yicone/84997bc9bc7452bbbde6b81297508fb9 to your computer and use it in GitHub Desktop.
Save yicone/84997bc9bc7452bbbde6b81297508fb9 to your computer and use it in GitHub Desktop.
基于枚举的FSM(有限状态机)
package com.yonyou.dmscloud.passengerflow.domain.model;
public class StateMachine {
ReceptionRecord context;
State state;
StateMachine(ReceptionRecord context) {
this.context = context;
this.state = State.Initial;
}
public enum Event {Submit, Overtime}
void submit() {
state.process(this, Event.Submit);
}
void overtime() {
state.process(this, Event.Overtime);
}
public enum State {
Initial {
@Override
void process(StateMachine sm, Event e) {
switch (e) {
case Submit:
this.exit(sm);
if (sm.context.getArrivalResult() == ReceptionRecord.ArrivalResult.无效) {
sm.state = Invalid;
} else {
// todo: [store.passengerFlow.enableAssign=true]/state=assigned
sm.state = Final;
}
sm.state.entry(sm);
break;
case Overtime:
this.exit(sm);
sm.state = Invalid;
sm.state.entry(sm);
break;
}
}
},
Invalid {
@Override
void process(StateMachine sm, Event e) {
}
},
Assigned {
@Override
void process(StateMachine sm, Event e) {
switch (e) {
case Submit:
this.exit(sm);
sm.state = Final;
sm.state.entry(sm);
break;
}
}
},
Final {
@Override
void process(StateMachine sm, Event e) {
}
};
abstract void process(StateMachine sm, Event e);
void entry(StateMachine sm) {
}
void exit(StateMachine sm) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment