案例-怎样用 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的机制关系不大,可忽略。


推荐阅读