您好,登錄后才能下訂單哦!
這篇文章主要講解了“squirrel-foundation有限狀態(tài)機(jī)的詳細(xì)介紹”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“squirrel-foundation有限狀態(tài)機(jī)的詳細(xì)介紹”吧!
有限狀態(tài)機(jī):是一種用來(lái)進(jìn)行對(duì)象行為建模的工具,其作用主要是描述對(duì)象在它的生命周期內(nèi)所經(jīng)歷的狀態(tài)序列,以及如何響應(yīng)來(lái)自外界的各種事件。在計(jì)算機(jī)科學(xué)中,有限狀態(tài)機(jī)被廣泛用于建模應(yīng)用行為、硬件電路系統(tǒng)設(shè)計(jì)、軟件工程,編譯器、網(wǎng)絡(luò)協(xié)議、和計(jì)算與語(yǔ)言的研究
本文主要學(xué)習(xí)java的一個(gè)開(kāi)源實(shí)現(xiàn) squirrel-foundation 狀態(tài)機(jī)
1 | 現(xiàn)態(tài)、條件、動(dòng)作、次態(tài)?!艾F(xiàn)態(tài)”和“條件”是因,“動(dòng)作”和“次態(tài)”是果 |
一般來(lái)說(shuō),隨著項(xiàng)目的發(fā)展,代碼變得越來(lái)越復(fù)雜,狀態(tài)之間的關(guān)系,轉(zhuǎn)化變得越來(lái)越復(fù)雜,比如訂單系統(tǒng)狀態(tài),活動(dòng)系統(tǒng)狀態(tài)等。產(chǎn)品或者開(kāi)發(fā)很難有一個(gè)人能夠梳理清楚整個(gè)狀態(tài)的全局. 使用狀態(tài)機(jī)來(lái)管理對(duì)象生命周期可以使得代碼具有更好的可讀性,可維護(hù)性,以及可測(cè)試性。
1 | 有限狀態(tài)機(jī)是一種對(duì)象行為建模工具,適用對(duì)象有一個(gè)明確并且復(fù)雜的生命流(一般而言三個(gè)以上狀態(tài)),并且在狀態(tài)變遷存在不同的觸發(fā)條件以及處理行為。 |
先來(lái)看squirrel-foundation github 上面給出的例子,對(duì)狀態(tài)機(jī)有一個(gè)直觀的認(rèn)識(shí)
添加maven 依賴(lài)
1 2 3 4 5 6 | <!-- 狀態(tài)機(jī)--> <dependency> <groupId>org.squirrelframework</groupId> <artifactId>squirrel-foundation</artifactId> <version>0.3.8</version> </dependency> |
QuickStartSample demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import org.squirrelframework.foundation.fsm.StateMachineBuilderFactory; import org.squirrelframework.foundation.fsm.UntypedStateMachine; import org.squirrelframework.foundation.fsm.UntypedStateMachineBuilder; import org.squirrelframework.foundation.fsm.annotation.StateMachineParameters; import org.squirrelframework.foundation.fsm.impl.AbstractUntypedStateMachine; /** * @Author wtx * @Date 2019/5/5 */ public class QuickStartSample { // 1. Define State Machine Event enum FSMEvent { ToA, ToB, ToC, ToD } // 2. Define State Machine Class @StateMachineParameters(stateType=String.class, eventType=FSMEvent.class, contextType=Integer.class) static class StateMachineSample extends AbstractUntypedStateMachine { protected void fromAToB(String from, String to, FSMEvent event, Integer context) { System.out.println("Transition from '"+from+"' to '"+to+"' on event '"+event+ "' with context '"+context+"'."); } protected void ontoB(String from, String to, FSMEvent event, Integer context) { System.out.println("Entry State \'"+to+"\'."); } } public static void main(String[] args) { // 3. Build State Transitions UntypedStateMachineBuilder builder = StateMachineBuilderFactory.create(StateMachineSample.class); builder.externalTransition().from("A").to("B").on(FSMEvent.ToB).callMethod("fromAToB"); builder.onEntry("B").callMethod("ontoB"); // 4. Use State Machine UntypedStateMachine fsm = builder.newStateMachine("A"); fsm.fire(FSMEvent.ToB, 10); System.out.println("Current state is "+fsm.getCurre5ntState()); } } |
首先使用StateMachineBuilderFactory構(gòu)造一個(gè)UntypedStateMachineBuilder,然后builder.externalTransition(),builder除了externalTransition還有internalTransition(),
.from(“A”).to(“B”).on(FSMEvent.ToB).callMethod(“fromAToB”);
當(dāng)狀態(tài)在A 發(fā)生 ToB 事件時(shí),調(diào)用 方法fromAToB,然后狀態(tài)轉(zhuǎn)化到 B。
builder.onEntry(“B”).callMethod(“ontoB”);
當(dāng)狀態(tài)轉(zhuǎn)移到B 時(shí),調(diào)用方法ontoB。
1 2 3 | UntypedStateMachine fsm = builder.newStateMachine("A"); // 觸發(fā)ToB事件 fsm.fire(FSMEvent.ToB, 10); |
1 | fsm.getCurrentState() |
感謝各位的閱讀,以上就是“squirrel-foundation有限狀態(tài)機(jī)的詳細(xì)介紹”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)squirrel-foundation有限狀態(tài)機(jī)的詳細(xì)介紹這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。