职责链实现的apache.chain使用
其实职责链在老早就使用过了,以前在HW给Vodafone做金融项目的时候,使用职责链完成交易步骤,那时觉得这东西真好用,可以直接通过配置决定业务流程,现在终于有机会实践一下。
这种设计模式本身的实现是非常容易的,可以简单单做是一组IF条件的集合,符合条件的继续传递;不符合条件的终止运行。chain代表了一条运行逻辑,就如同一条项链,我们的业务逻辑就如同是珍珠,并且都实现了同样的compute接口。apache的实现,是通过将数据封装到上下文(context)中,而且该上下文就是串起这些珍珠的金线。
下面是自己写的一段例子:
链的组织,也可以通过配置xml文件来实现,用在spring框架中非常合适。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * 职责链的组织类,负责构造整个链 */ public class RootCauseChain extends ChainBase { /** * 通过此方法增减生效的分析器 */ public RootCauseChain() { addCommand( new DataRootCauseAnalyzer()); //addCommand(new EnvRootCauseAnalyzer()); //addCommand(new PifRootCauseAnalyzer()); //addCommand(new TaskRootCauseAnalyzer()); } } |
具体的业务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | /** * 实现了command接口,数据均通过context组织 */ public class DataRootCauseAnalyzer implements Command { private DataQueryService dqService = new DataQueryService(); private static final String ROOT_CAUSE_FORMAT = "indicator value is abnormal: check ? for more information" ; @SuppressWarnings ( "unchecked" ) @Override public boolean execute(Context arg0) throws Exception { boolean res = false ; Log.info(RootCauseConstant.MODULE_CODE, "0000" , "begin to execute DataRootCauseAnalyzer.execute" ); List<DataPoint> exceptionDataPoints = (List<DataPoint>) arg0.get( "expData" ); ExceptionRule exceptionRule = (ExceptionRule) arg0.get( "rule" ); List<RootCause> result = new ArrayList<RootCause>(); if (exceptionDataPoints != null && !exceptionDataPoints.isEmpty()) { for (DataPoint dataPoint : exceptionDataPoints) { List<RootCause> rootCauseList = generateRootCause(dataPoint, exceptionRule); result.addAll(rootCauseList); } } // 如果分析出了根因,则结束分析流程 if (result != null && !result.isEmpty()) { arg0.put( "result" , result); res = true ; } // 没有分析出根因,交到下一个分析器进行分析 return res; } /** * 生成具体的异常信息 * * @param exceptionPoint * 异常数据点 * @param rule * 异常规则 * @return 查询上下级关系 */ private List<RootCause> generateRootCause(DataPoint exceptionPoint, ExceptionRule rule) { List<RootCause> rclist = new ArrayList<RootCause>(); return rclist; } } |
调用:
public class RootCauseService { /** * 分析异常点的根因 * * @param points * 异常数据点 * @param rule * 异常数据发现规则 * @return 异常数据点及根因 */ @SuppressWarnings("unchecked") public List<RootCause> getRootCause(List<DataPoint> points, ExceptionRule rule) { Log.info(RootCauseConstant.MODULE_CODE, "0000", "begin to execute getRootCause, points=" + points + ", rule=" + rule); List<RootCause> result = new ArrayList<RootCause>(); try { Command command = new RootCauseChain(); ContextBase ctx = new ContextBase(); ctx.put("expData", points); ctx.put("rule", rule); command.execute(ctx); result = (List<RootCause>) ctx.get("result"); } catch (Exception e) { Log.error(RootCauseConstant.MODULE_CODE, "0000", "execute analysisRootCauseByCommandChain exception.", e); } Log.info(RootCauseConstant.MODULE_CODE, "0000", "execute getRootCause finished, result=" + result); return result; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述