Squirrel狀態機是一種用來進行物件行為建模的工具,主要描述物件在它的生命週期內所經歷的狀態,以及如何響應來自外界的各種事件。比如訂單的建立、已支付、發貨、收穫、取消等等狀態、狀態之間的控制、觸發事件的監聽,可以用該框架進行清晰的管理實現。使用狀態機來管理物件生命流的好處更多體現在程式碼的可維護性、可測試性上,明確的狀態條件、原子的響應動作、事件驅動遷移目標狀態,對於流程複雜易變的業務場景能大大減輕維護和測試的難度。
Squirrel狀態機是一種有限狀態機,有限狀態機是指物件有一個明確並且複雜的生命流(一般而言三個以上狀態),並且在狀態變遷存在不同的觸發條件以及處理行為。
Squirrel狀態機可歸納為4個要素,即現態、條件、動作、次態。「現態」和「條件」是因,「動作」和「次態」是果。
舉例,京東線上開店需要經過稽核才能正式上線,店鋪狀態有待稽核、已駁回、已稽核,對應操作有提交稽核,稽核通過,稽核駁回動作。現在需要實現一個店鋪稽核流程的需求。
圖1.if-else/switch-case模式實現流程圖
圖2.狀態機模式實現流程圖
通過引入狀態機,可以去除大量if-else if-else或者switch-case分支結構,直接通過當前狀態和狀態驅動表查詢行為驅動表,找到具體行為執行操作,有利於程式碼的維護和擴充套件。
圖3.狀態機建立流程圖
<dependency>
<groupId>org.squirrelframework</groupId>
<artifactId>squirrel-foundation</artifactId>
<version>0.3.9</version>
</dependency>
// 店鋪稽核狀態
public Enum ShopInfoAuditStatusEnum{
audit(0,"待稽核"),
agree(1,"稽核通過"),
reject(2,"稽核駁回");
}
// 店鋪稽核事件
public Enum ShopInfoAuditEvent{
SUBMIT, // 提交
AGREE, // 同意
REJECT; // 駁回
}
/**
* StateMachineBuilder範例
*/
public class StateMachineEngine <T extends UntypedStateMachine, S, E, C> implements ApplicationContextAware{
private ApplicationContext applicationContext;
private static Map<String,UntypedStateMachineBuilder> builderMap = new HashMap<String,UntypedStateMachineBuilder>();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Transactional
public void fire(Class<T> machine, S state, E event, C context) {
StateMachineBuilder stateMachineBuilder = this.getStateMachineBuilder(machine);
StateMachine stateMachine = stateMachineBuilder.newStateMachine(state,applicationContext);
stateMachine.fire(event, context);
}
private StateMachineBuilder getStateMachineBuilder(Class<T> stateMachine){
UntypedStateMachineBuilder stateMachineBuilder = builderMap.get(stateMachine.getName());
if(stateMachineBuilder == null){
stateMachineBuilder = StateMachineBuilderFactory.create(stateMachine,ApplicationContext.class);
builderMap.put(stateMachine.getName(),stateMachineBuilder);
}
return stateMachineBuilder;
/**
* 店鋪稽核狀態機
*/
@States({
@State(name = "audit"),
@State(name = "agree"),
@State(name = "reject")
})
@Transitions({
@Transit(from = "audit", to = "agree", on = "AGREE", callMethod = "agree"),
@Transit(from = "audit", to = "reject", on = "REJECT", callMethod = "reject"),
@Transit(from = "reject", to = "audit", on = "SUBMIT", callMethod = "submit"),
@Transit(from = "agree", to = "audit", on = "SUBMIT", callMethod = "submit"),
@Transit(from = "audit", to = "audit", on = "SUBMIT", callMethod = "submit"),
})
@StateMachineParameters(stateType=ShopInfoAuditStatusEnum.class, eventType=ShopInfoAuditEvent.class, contextType=ShopInfoAuditStatusUpdateParam.class)
public class ShopInfoAuditStateMachine extends AbstractUntypedStateMachine {
private ApplicationContext applicationContext;
public ShopInfoAuditStateMachine(){}
public ShopInfoAuditStateMachine(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
// 稽核通過業務邏輯
public void agree(ShopInfoAuditStatusEnum fromState, ShopInfoAuditStatusEnum toState, ShopInfoAuditEvent event, ShopInfoAuditStatusUpdateParam param) {
this.agree(fromState,toState,event,param);
}
// 稽核駁回業務邏輯
public void reject(ShopInfoAuditStatusEnum fromState, ShopInfoAuditStatusEnum toState, ShopInfoAuditEvent event, ShopInfoAuditStatusUpdateParam param) {
this.reject(fromState,toState,event,param);
}
// 提交業務邏輯
public void submit(ShopInfoAuditStatusEnum fromState, ShopInfoAuditStatusEnum toState, ShopInfoAuditEvent event, ShopInfoAuditStatusUpdateParam param) {
this.submit(fromState,toState,event,param);
}
// 呼叫端
main{
StateMachineEngine stateMachineEngine = applicationContext.getBean(StateMachineEngine.class);
// 稽核通過調case
stateMachineEngine.fire(ShopInfoAuditStateMachine.class,ShopInfoAuditStatusEnum.audit,ShopInfoAuditEvent.AGREE,param);
// 稽核駁回case
stateMachineEngine.fire(ShopInfoAuditStateMachine.class,ShopInfoAuditStatusEnum.audit,ShopInfoAuditEvent.REJECT,param);
}
狀態機很好的幫我們處理了物件狀態的流轉、事件的監聽以及外界的各種事件的響應。從程式碼設計角度減少了大量if-else/switch-case邏輯判斷,提高了程式碼的可維護性、擴充套件性,方便管理和測試。