Created
June 2, 2020 07:00
-
-
Save yicone/84997bc9bc7452bbbde6b81297508fb9 to your computer and use it in GitHub Desktop.
基于枚举的FSM(有限状态机)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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