回顾:
在上一篇文章
中,我们介绍了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 FLOWER
OUT FROM :SMALL
currentState-->ATTACH
Process 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 FLOWER
OUT FROM :SMALL
currentState-->ATTACH
2017-12-27 23:41:32.896 myAppName [main] INFO com.github.oxo42.stateless4j.StateMachine - Firing MONSTER
ENTRY TO : SMALL
Process finished with exit code 0
该包给我们提供了可以传入不同个数参数的Action及Action1,Action2... ...等接口,可以适应不同场合的需要去实现;
其中Transition对象为一个包含变化前后state和当前trigger对象,通过get方法获得;
另外还提供了Func接口和BooleanFunc接口,需要的时候可以使用;