博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA -- stateless4j StateMachine 使用浅析(二)
阅读量:6867 次
发布时间:2019-06-26

本文共 3925 字,大约阅读时间需要 13 分钟。

hot3.png

回顾:

在上一篇文章<JAVA -- stateless4j StateMachine 使用浅析(一)> 

中,我们介绍了stateless4j statemachine的使用入门,这一篇我们介绍 Exit && Entry Action;

废话不多说,show you the code:

import com.github.oxo42.stateless4j.StateMachineConfig;/** * Created by Administrator on 2017/12/26. * * state和trigger注册 */public class StateConver {    public static StateMachineConfig
config = new StateMachineConfig<>(); static { /** * 最初为small状态时 */ config.configure(CurrentState.SMALL) /** * 从当前状态改变时所触发的动作 */ .onExit(new ExitAction()) /** * 改变到当前状态时所触发的动作 */ .onEntry(new EntryAction()) /** * 遇到蘑菇触发-->big状态 */ .permit(Trigger.MUSHROOM,CurrentState.BIG) /** * 花朵触发,-->直接变为可攻击状态 */ .permit(Trigger.FLOWER,CurrentState.ATTACH) /** * 妖怪触发,死亡状态 */ .permit(Trigger.MONSTER,CurrentState.DEAD); /** * 最初为big状态 */ config.configure(CurrentState.BIG) /** * 蘑菇触发,状态不变, * permitReentry方法state变化相同,但具体执行过程有一些区别 */ .ignore(Trigger.MUSHROOM) .permit(Trigger.FLOWER,CurrentState.ATTACH) .permit(Trigger.MONSTER,CurrentState.SMALL); config.configure(CurrentState.ATTACH) .ignore(Trigger.MUSHROOM) .ignore(Trigger.FLOWER) .permit(Trigger.MONSTER,CurrentState.SMALL); config.configure(CurrentState.DEAD) .ignore(Trigger.MUSHROOM) .ignore(Trigger.FLOWER) .ignore(Trigger.MONSTER); }}
import com.github.oxo42.stateless4j.delegates.Action1;import com.github.oxo42.stateless4j.transitions.Transition;/** * Created by Administrator on 2017/12/27. *  * ExitAction实现类 */public class ExitAction implements Action1
> { @Override public void doIt(Transition
arg1) { System.out.println("OUT FROM :" + arg1.getSource()); }}
import com.github.oxo42.stateless4j.delegates.Action1;import com.github.oxo42.stateless4j.transitions.Transition;/** * Created by Administrator on 2017/12/27. *  * EntryAction实现类 */public class EntryAction implements Action1
> { @Override public void doIt(Transition
arg1) { System.out.println("ENTRY TO : " +arg1.getDestination()); }}
/** * Created by Administrator on 2017/12/26. * * 状态机测试类 */public class RunStateMachine {    private static StateMachine
stateMachine = new StateMachine
(CurrentState.SMALL,StateConver.config); @Test public void testStateMachine(){ stateMachine.fire(Trigger.FLOWER); System.out.println("currentState-->"+stateMachine.getState()); }}

依旧运行这个测试类,查看输出日志为:

2017-12-27 23:30:57.469 myAppName [main] INFO  com.github.oxo42.stateless4j.StateMachine - Firing FLOWEROUT FROM :SMALLcurrentState-->ATTACHProcess finished with exit code 0

成功触发exit动作;

同样的,我们可以触发一个EntryAction动作,

@Test    public void testStateMachine(){        stateMachine.fire(Trigger.FLOWER);        System.out.println("currentState-->"+stateMachine.getState());        stateMachine.fire(Trigger.MONSTER);    }
2017-12-27 23:41:32.893 myAppName [main] INFO  com.github.oxo42.stateless4j.StateMachine - Firing FLOWEROUT FROM :SMALLcurrentState-->ATTACH2017-12-27 23:41:32.896 myAppName [main] INFO  com.github.oxo42.stateless4j.StateMachine - Firing MONSTERENTRY TO : SMALLProcess finished with exit code 0

该包给我们提供了可以传入不同个数参数的Action及Action1,Action2... ...等接口,可以适应不同场合的需要去实现;

其中Transition对象为一个包含变化前后state和当前trigger对象,通过get方法获得;

另外还提供了Func接口和BooleanFunc接口,需要的时候可以使用;

转载于:https://my.oschina.net/JackieRiver/blog/1595876

你可能感兴趣的文章
web前端规范
查看>>
理解网页的关键渲染路径(CRP)
查看>>
使用vue-cli脚手架+webpack搭建vue项目
查看>>
Docker - 03 编排容器 Docker Compose 指令速查表
查看>>
Mybatis基本映射--INSERT
查看>>
移动 web 端屏幕适配 - rem
查看>>
聊聊hystrix的BucketedCounterStream
查看>>
50多种适合机器学习和预测应用的API,你的选择是?(2018年版本)
查看>>
【题目】【4天】寻宝
查看>>
Flutter教程(一) 十分钟了解Flutter
查看>>
maven实战第一步,eclipse创建hello-world项目
查看>>
安装自动化工具ansible
查看>>
手把手教你理解卷积神经网络
查看>>
本地安装sass出错问题解析
查看>>
vue项目优化--使用CDN和Gzip
查看>>
JS练习实例--编写经典小游戏俄罗斯方块
查看>>
简述Linux的启动过程
查看>>
fir.im Weekly - 如何写出零 bug 的代码
查看>>
springboot+postgresql+docker实例
查看>>
[LeetCode] Reverse Vowels of a String
查看>>