首页 > 网络 > 网络热点

管理订单状态,该上状态机吗?

常驻编辑 网络热点 2022-06-05 连贯   状态   嵌套   状态机   线程   订单   接口   原理   代码   方法   项目
stateMachine = builder.build(MACHINE_ID); States target = stateMachine.fireEvent(States.STATE1, Events.EVENT1, new Context()); Assert.assertEquals(States.STATE2, target); } private Condition checkCondition() { return (ctx) -> {return true;}; } private Action doAction() { return (from, to, event, ctx)->{ System.out.println(ctx.operator+" is operating "+ctx.entityId+" from:"+from+" to:"+to+" on:"+event); }; }

可以看到,每次进行状态流转时,检查checkCondition(),当返回true,执行状态流转的操作doAction()。5zq拜客生活常识网

后面所有的checkCondition()和doAction()方法在下方就不再重复贴出了。5zq拜客生活常识网

  1. 从多个状态流传到新的状态
@Test
public void testExternalTransitionsNormal(){
    StateMachineBuilder builder = StateMachineBuilderFactory.create();
    builder.externalTransitions()
            .fromAmong(States.STATE1, States.STATE2, States.STATE3)
            .to(States.STATE4)
            .on(Events.EVENT1)
            .when(checkCondition())
            .perform(doAction());

    StateMachine stateMachine = builder.build(MACHINE_ID+"1");
    States target = stateMachine.fireEvent(States.STATE2, Events.EVENT1, new Context());
    Assert.assertEquals(States.STATE4, target);
}
  1. 状态内部触发流转
@Test
public void testInternalNormal(){
    StateMachineBuilder builder = StateMachineBuilderFactory.create();
    builder.internalTransition()
            .within(States.STATE1)
            .on(Events.INTERNAL_EVENT)
            .when(checkCondition())
            .perform(doAction());
    StateMachine stateMachine = builder.build(MACHINE_ID+"2");

    stateMachine.fireEvent(States.STATE1, Events.EVENT1, new Context());
    States target = stateMachine.fireEvent(States.STATE1, Events.INTERNAL_EVENT, new Context());
    Assert.assertEquals(States.STATE1, target);
}
  1. 多线程测试并发测试
@Test
public void testMultiThread(){
 buildStateMachine("testMultiThread");

  for(int i=0 ; i<10 ; i++){
   Thread thread = new Thread(()->{
      StateMachine stateMachine = StateMachineFactory.get("testMultiThread");
      States target = stateMachine.fireEvent(States.STATE1, Events.EVENT1, new Context());
      Assert.assertEquals(States.STATE2, target);
      });
      thread.start();
    }


    for(int i=0 ; i<10 ; i++) {
      Thread thread = new Thread(() -> {
      StateMachine stateMachine = StateMachineFactory.get("testMultiThread");
      States target = stateMachine.fireEvent(States.STATE1, Events.EVENT4, new Context());
      Assert.assertEquals(States.STATE4, target);
      });
      thread.start();
    }

    for(int i=0 ; i<10 ; i++) {
      Thread thread = new Thread(() -> {
      StateMachine stateMachine = StateMachineFactory.get("testMultiThread");
      States target = stateMachine.fireEvent(States.STATE1, Events.EVENT3, new Context());
      Assert.assertEquals(States.STATE3, target);
      });
      thread.start();
  }

}

由于COLA状态机时无状态的状态机,所以性能是很高的。相比起来,SpringStateMachine由于是有状态的,就需要使用者自行保证线程安全了。5zq拜客生活常识网

二、多分支状态流转示例

/**
* 测试选择分支,针对同一个事件:EVENT1
* if condition == "1", STATE1 --> STATE1
* if condition == "2" , STATE1 --> STATE2
* if condition == "3" , STATE1 --> STATE3
*/
@Test
public void testChoice(){
  StateMachineBuilder builder = StateMachineBuilderFactory.create();
  builder.internalTransition()
  .within(StateMachineTest.States.STATE1)
  .on(StateMachineTest.Events.EVENT1)
  .when(checkCondition1())
  .perform(doAction());
  builder.externalTransition()
  .from(StateMachineTest.States.STATE1)
  .to(StateMachineTest.States.STATE2)
  .on(StateMachineTest.Events.EVENT1)
  .when(checkCondition2())
  .perform(doAction());
  builder.externalTransition()
  .from(StateMachineTest.States.STATE1)
  .to(StateMachineTest.States.STATE3)
  .on(StateMachineTest.Events.EVENT1)
  .when(checkCondition3())
  .perform(doAction());

  StateMachine    

相关阅读:

  • 尤伯杯陈雨菲开门红,何冰娇三局险胜,中国队横扫印尼进四
  • 当前正在热播的六部剧,《破事精英》排第三,你在追哪一部
  • 女孩醉心武侠,自导自演成一派,这表演看着过瘾
  • 第74集团军某旅组织侦察专业跨昼夜连贯考核——大雨未
  • A股,或将迎来一次大洗牌
  • 第74集团军某旅组织侦察专业跨昼夜连贯考核
  • 陪你笑,看你闹
  • 酒局小游戏,无道具(3.0)
  • 西媒:超仿真机器人能表达感情
  • 8868体育:快速崩盘,诺丁汉四连败
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。