案例-怎样用 UML 设计一个简单的工作流引擎(含递归算法)?( 四 )
public class SimpleRouter extends AbstractRouter { public SimpleRouter(Tuple2 nextStepWithRouter) { super(nextStepWithRouter); } @Override public StepState route(Map\u0026lt;String, Object\u0026gt; context, StepState preStepState) throws Exception { StepState currentStepState ; if (_FLOW_CONTINUE_FLAG == preStepState.getContinueFlag() \u0026amp;\u0026amp; nextStepWithRouter != null) { IStep step = (IStep) nextStepWithRouter.getFirst(); IRouter router = (IRouter) nextStepWithRouter.getSecond(); currentStepState = step.run(context); //递归执行后续步骤 return router.route(context, currentStepState); } else { currentStepState = preStepState; } return currentStepState; }}
2、ConditionRouterpublic class ConditionRouter extends AbstractRouter { private Logger logger = LoggerFactory.getLogger(SimpleRouter.class); public ConditionRouter(Tuple2 nextStepWithRouter) { super(nextStepWithRouter); } @Override public StepState route(Map\u0026lt;String, Object\u0026gt; context, StepState preStepState) throws Exception { StepState currentStepState; if (_FLOW_CONTINUE_FLAG == preStepState.getContinueFlag() \u0026amp;\u0026amp; nextStepWithRouter != null) { Map\u0026lt;IChecker\u0026lt;String\u0026gt;, Flow\u0026gt; flows = (Map\u0026lt;IChecker\u0026lt;String\u0026gt;, Flow\u0026gt;) nextStepWithRouter.getFirst(); IRouter router = (IRouter) nextStepWithRouter.getSecond(); //寻找与当前StepState的forkFlag相符的分支 Flow forkFlow = null; Set\u0026lt;Map.Entry\u0026lt;IChecker\u0026lt;String\u0026gt;, Flow\u0026gt;\u0026gt; entrySet = flows.entrySet(); for (Map.Entry\u0026lt;IChecker\u0026lt;String\u0026gt;, Flow\u0026gt; entry : entrySet) { IChecker checker = entry.getKey(); if (checker.check(preStepState.getForkFlag())) { forkFlow = entry.getValue(); break; } } //若子分支存在执行流程 if (forkFlow == null) { logger.info("步骤名称为的step, forkFlag参数未指定有效值,故跳过分支执行后续步骤"); currentStepState = preStepState; } else { currentStepState = forkFlow.run(context); } //递归执行后续步骤 return router.route(context, currentStepState); } else { currentStepState = preStepState; } return currentStepState; }}
context是用于步骤间传值的上下文。与router的机制关系不大,可忽略。
推荐阅读
- 聪明人养花,这3种“花”怎样也要养一盆,每年能省不少医药费
- 湖北通报扫黑除恶专项斗争典型案例办理情况
- 冬季煤气中毒案例多广东发出风险提醒
- 互联网怎样解决“家政服务上门速度慢”的问题
- 怎样看待从1月8号起,QQ钱包开始提现收费
- 银行it人怎样转型
- 汽车|冬天怎样让车内温度快速升高?座椅加热的最佳使用方式二,外循环的作用总结
- 怎样进入通信行业
- 怎样评价扶他柠檬茶的小说《云养汉》的结尾
- 怎样成为一名合格的Python程序员?