当前位置:   article > 正文

设计模式-组合模式(决策树)_decisonmatter

decisonmatter

一、只如初见

        组合模式也许大家第一联想到的就是把两个模块组合起来使用,其实好像是这样也其实不是这样,废话不多说,学习一件新的事物总要先了解一下他的概念,老规矩先上概念(摘自百度百科):

组合模式_百度百科组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构。https://baike.baidu.com/item/%E7%BB%84%E5%90%88%E6%A8%A1%E5%BC%8F/1441281?fromModule=lemma_inlink

       看完概念想了一下完全和自己想的不一样,最近在看重学Java设计模式其中就讲到了这篇设计模式,其中讲的很容易理解(没有官方的晦涩哈),把其中的精华拿出来与大家分享;给大家的建议就是可能看一遍觉得似懂非懂,但是写一遍就会发现茅塞顿开。

二、登堂入室

        相信大家都玩过积木或者乐高玩具,其中每个零部件都是有自己的职责以及功能的。比如说小汽车光有轮子他是无法行走的,必须得有发动机,底盘,车身,电器设备等等部件组成。

        像这种利用ABCD功能组合对外提供服务就类似组合模式。对于代码实现有什么特点?那么这使用有他对于项目有什么好处呢?对于加入他对于团队有什么好处呢?灵魂三问,也许你也有类似的问题在脑海中循环,就像这样子。

开个玩笑让大家紧绷的思维放松一下,下面言归正传。

三、抽丝剥茧

3.1 实现特点

什么?你看完这个还是有点迷糊。确实这样有点官方,如果这样不太好理解可以看下这个图:

这样大概就好解释了,假设产品有一个业务诉求先找到了经理,经理说我知道谁能做这个事,找到了A组长,A组长了解了下需求把具体的任务分配到了对应的工程师;相信看完我说的这些各位大概心里有点朦胧的概念了,已经跃跃欲试了;别着急,我们接着往下看。

3.2加入项目中的优势/对于团队好处

        前面我们了解到这种设计模式其实就是一个类似树型结构,递归去查找对应的功能,可以让代码易于扩展和维护,更降低团队的维护成本,最重要的是避免接班的伙伴的吐槽。这时候就有人反驳了,我觉得不尽然,这玩意不就是我加个if else的事么需要搞的这么高深莫测么?有疑问是正常的,我刚开始看的时候也有类似的疑问不过相信大家看完后面的就觉得不会有类似的疑问了。

四、举一反三

        前面我们讲到其实这种设计模式也没啥好处,对于实现功能来说就是一个if else 的事,下面我们就来举个例子让大家思考下为什么要用这个例子。

4.1   反例     

下面以一个产品与研发的实际业务场景来举例:

日期需求程序员(内心独白)
星期一(早上)哥哥,业务说有一个紧急需求,要在这里加一个判断根据不同渠道发放不同激励。行吧。(也不难,这里加个判断就能上线)

星期二

(下午)

哥哥,上线效果非常不错,业务说要按不同业务类型的在细分下,激励团队。也不难。(加个类型判断上线吧)

星期三

(晚上)

喂,睡了吗?业务说这次搞的划分非常不错,还要在细分下按照团队销量来细分下。行吧,加。(已经意识到 if else有点多了)

星期四

(凌晨)

哇,你们太棒了,上线真快,有一个小要求,需要调整下根据团队中个人销量来细分下激励。好吧,加。(一大堆if else 要修改)

星期五

(半夜)

喂,坏了,怎么发的激励不对了,业务那边开始投诉了,你快看看是什么问题。(留下了悔恨的泪水)

代码实现:

  1. public class EngineController {
  2. private Logger logger = LoggerFactory.getLogger(EngineController.class);
  3. public String process(final String channelId, final String userId, final BigDecimal teamSalesVolume,final BigDecimal salesVolume) {
  4. logger.info("ifelse实现方式判断用户结果。channelId:{} userId:{} teamSalesVolume:{} salesVolume:{}", channelId, userId, teamSalesVolume,salesVolume);
  5. if ("0001".equals(channelId)) {
  6. if (new BigDecimal("1000").compareTo(teamSalesVolume)>0||new BigDecimal("1000").compareTo(salesVolume)>0) {
  7. return "奖励A";
  8. }
  9. if (new BigDecimal("1000").compareTo(teamSalesVolume)<=0||new BigDecimal("1000").compareTo(salesVolume)<=0) {
  10. return "奖励B";
  11. }
  12. }
  13. if ("0002".equals(channelId)) {
  14. if (new BigDecimal("1000").compareTo(teamSalesVolume)>0||new BigDecimal("1000").compareTo(salesVolume)>0) {
  15. return "奖励C";
  16. }
  17. if (new BigDecimal("1000").compareTo(teamSalesVolume)<=0||new BigDecimal("1000").compareTo(salesVolume)<=0) {
  18. return "奖励D";
  19. }
  20. }
  21. return null;
  22. }
  23. }

        从上面的结果来看无疑是最快的方式实现了对应的逻辑,但是后续如果if else 太多了就会变成最终的烂代码对后续的维护和迭代造成不同程度的影响。

4.2 正例

工程目录

下面我们开始按层级讲解对应模块功能以及作用 

4.2.1 LogicFilter原始过滤接口

LogicFilter原始过滤接口,定义了原始行为方法,逻辑决策器方法,获取决策值方法,每一个提供决策能力的节点必须实现此接口。

  1. package cn.test.design.domain.service.logic;
  2. import java.util.List;
  3. import java.util.Map;
  4. import cn.test.design.domain.model.vo.TreeNodeLink;
  5. /**
  6. * 原始决策过滤器
  7. */
  8. public interface LogicFilter {
  9. /**
  10. * 逻辑决策器
  11. *
  12. * @param matterValue 决策值
  13. * @param treeNodeLineInfoList 决策节点
  14. * @return 下一个节点Id
  15. */
  16. Long filter(String matterValue, List<TreeNodeLink> treeNodeLineInfoList);
  17. /**
  18. * 获取决策值
  19. *
  20. * @param decisionMatter 决策物料
  21. * @return 决策值
  22. */
  23. String matterValue(Long treeId, String userId, Map<String, String> decisionMatter);
  24. }

4.2.2 BaseLogic抽象基本实现

BaseLogic:抽象基本实现了LogicFilter 中基本的决策逻辑根据不同类型大于等于,小于等判断逻辑,同时定义了抽象方法让实现他的类都必须实现获取决策值方法,这个决策值用于逻辑对比。

  1. package cn.test.design.domain.service.logic;
  2. import java.util.List;
  3. import java.util.Map;
  4. import cn.test.design.domain.model.vo.TreeNodeLink;
  5. /**
  6. * 过滤器抽象基本实现实现,实现基本基本决策逻辑
  7. */
  8. public abstract class BaseLogic implements LogicFilter {
  9. /**
  10. * 实现基本基本决策逻辑
  11. */
  12. @Override
  13. public Long filter(String matterValue, List<TreeNodeLink> treeNodeLinkList) {
  14. for (TreeNodeLink nodeLine : treeNodeLinkList) {
  15. if (decisionLogic(matterValue, nodeLine)) return nodeLine.getNodeIdTo();
  16. }
  17. return 0L;
  18. }
  19. /**
  20. * 让子类必须实现获取决策值的方法用于对比获取最终的结果
  21. */
  22. @Override
  23. public abstract String matterValue(Long treeId, String userId, Map<String, String> decisionMatter);
  24. private boolean decisionLogic(String matterValue, TreeNodeLink nodeLink) {
  25. switch (nodeLink.getRuleLimitType()) {
  26. case 1:
  27. return matterValue.equals(nodeLink.getRuleLimitValue());
  28. case 2:
  29. return Double.parseDouble(matterValue) > Double.parseDouble(nodeLink.getRuleLimitValue());
  30. case 3:
  31. return Double.parseDouble(matterValue) < Double.parseDouble(nodeLink.getRuleLimitValue());
  32. case 4:
  33. return Double.parseDouble(matterValue) <= Double.parseDouble(nodeLink.getRuleLimitValue());
  34. case 5:
  35. return Double.parseDouble(matterValue) >= Double.parseDouble(nodeLink.getRuleLimitValue());
  36. default:
  37. return false;
  38. }
  39. }
  40. }

4.2.3 树根节点实现(ChannelGenderFilter,UserSalesVolumeFilter)

这里我们目前业务场景只用到了渠道与销售额所以目前只定义了两种如果实际需要可以定义更多哦;当然目前是直接获取物料值,实际开发中肯定是根据配置文件或从rpc接口或从数据库中获取对应需要的信息。

ChannelGenderFilter:渠道节点

  1. package cn.test.design.domain.service.logic.impl;
  2. import java.util.Map;
  3. import cn.test.design.domain.service.logic.BaseLogic;
  4. /**
  5. *
  6. * 渠道基本过滤器
  7. *
  8. */
  9. public class ChannelGenderFilter extends BaseLogic {
  10. @Override
  11. public String matterValue(Long treeId, String userId, Map<String, String> decisionMatter) {
  12. // 目前是直接获取物料值,实际开发中肯定是根据配置文件或从rpc接口或从数据库中获取对应需要的信息。
  13. return decisionMatter.get("channel");
  14. }
  15. }

UserSalesVolumeFilter:用户销售额

  1. package cn.test.design.domain.service.logic.impl;
  2. import java.util.Map;
  3. import cn.test.design.domain.service.logic.BaseLogic;
  4. /**
  5. * 用户销售额过滤器
  6. *
  7. */
  8. public class UserSalesVolumeFilter extends BaseLogic {
  9. @Override
  10. public String matterValue(Long treeId, String userId, Map<String, String> decisionMatter) {
  11. // 目前是直接获取物料值,实际开发中肯定是根据配置文件或从rpc接口或从数据库中获取对应需要的信息.
  12. return decisionMatter.get("userSalesVolume");
  13. }
  14. }

4.2.4 IEngine决策引擎

        提供给调用方的决策引擎接口,定义好了对应的方法后续如果有新的决策逻辑可以实现对应方法。

  1. package cn.test.design.domain.service.engine;
  2. import java.util.Map;
  3. import cn.test.design.domain.model.aggregates.TreeRich;
  4. import cn.test.design.domain.model.vo.EngineResult;
  5. /**
  6. * 决策引擎接口
  7. *
  8. */
  9. public interface IEngine {
  10. EngineResult process(final Long treeId, final String userId, TreeRich treeRich, final Map<String, String> decisionMatter);
  11. }

4.2.5 EngineConfig 决策节点配置

        这里的决策配置目前是固定的两个树根节点实现(ChannelGenderFilter,UserSalesVolumeFilter)实际场景是把需要的实现配置到表中,可以方便在运营后台配置。

  1. package cn.test.design.domain.service.engine;
  2. import java.util.Map;
  3. import java.util.concurrent.ConcurrentHashMap;
  4. import cn.test.design.domain.service.logic.LogicFilter;
  5. import cn.test.design.domain.service.logic.impl.ChannelGenderFilter;
  6. import cn.test.design.domain.service.logic.impl.UserSalesVolumeFilter;
  7. /**
  8. *
  9. * 决策节点配置,此处可以配置至数据库中方便后续界面操作维护
  10. *
  11. */
  12. public class EngineConfig {
  13. static Map<String, LogicFilter> logicFilterMap;
  14. static {
  15. logicFilterMap = new ConcurrentHashMap<>();
  16. logicFilterMap.put("channel", new ChannelGenderFilter());
  17. logicFilterMap.put("userSalesVolume", new UserSalesVolumeFilter());
  18. }
  19. public Map<String, LogicFilter> getLogicFilterMap() {
  20. return logicFilterMap;
  21. }
  22. @SuppressWarnings("static-access")
  23. public void setLogicFilterMap(Map<String, LogicFilter> logicFilterMap) {
  24. this.logicFilterMap = logicFilterMap;
  25. }
  26. }

4.2.6 EngineBase 基础决策引擎

        这里是主要决策树处理流程,有点像找树叶的果实一样。同时实现了IEngine 基础决策引擎

但是只是提供了抽像并没有实现,由最终的决策实现类引擎实现。

  1. package cn.test.design.domain.service.engine;
  2. import java.util.Map;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import cn.test.design.domain.model.aggregates.TreeRich;
  6. import cn.test.design.domain.model.vo.EngineResult;
  7. import cn.test.design.domain.model.vo.TreeNode;
  8. import cn.test.design.domain.model.vo.TreeRoot;
  9. import cn.test.design.domain.service.logic.LogicFilter;
  10. /**
  11. * 决策树引擎
  12. */
  13. public abstract class EngineBase extends EngineConfig implements IEngine {
  14. private Logger logger = LoggerFactory.getLogger(EngineBase.class);
  15. @Override
  16. public abstract EngineResult process(Long treeId, String userId, TreeRich treeRich, Map<String, String> decisionMatter);
  17. protected TreeNode engineDecisionMaker(TreeRich treeRich, Long treeId, String userId, Map<String, String> decisionMatter) {
  18. TreeRoot treeRoot = treeRich.getTreeRoot();
  19. Map<Long, TreeNode> treeNodeMap = treeRich.getTreeNodeMap();
  20. // 规则树根ID
  21. Long rootNodeId = treeRoot.getTreeRootNodeId();
  22. TreeNode treeNodeInfo = treeNodeMap.get(rootNodeId);
  23. //循环获取对应的处理节点直到获取到对应的结果
  24. //节点类型[NodeType];1子叶、2果实 我们只需要使用果实
  25. while (treeNodeInfo.getNodeType().equals(1)) {
  26. //对应处理规则key用于获取对应实现LogicFilter 的处理节点主要用来获取物料中用于对比的相关值
  27. String ruleKey = treeNodeInfo.getRuleKey();
  28. //实际的处理节点
  29. LogicFilter logicFilter = logicFilterMap.get(ruleKey);
  30. String matterValue = logicFilter.matterValue(treeId, userId, decisionMatter);
  31. //基础决策抽象中的能力,使用物料值判断使用那个决策节点(获取的是下一个节点)
  32. Long nextNode = logicFilter.filter(matterValue, treeNodeInfo.getTreeNodeLinkList());
  33. //赋值对应的处理节点,直到找到期望的处理节点(我们这里是最终的果实节点)
  34. treeNodeInfo = treeNodeMap.get(nextNode);
  35. logger.info("决策树引擎=>{} userId:{} treeId:{} treeNode:{} ruleKey:{} matterValue:{}", treeRoot.getTreeName(), userId, treeId, treeNodeInfo.getTreeNodeId(), ruleKey, matterValue);
  36. }
  37. return treeNodeInfo;
  38. }
  39. }

4.2.7 TreeEngineHandle 基础决策引擎实现

        这里基本都比较简单直接使用用户传递的对应值传入对应方法即可获取到对应结果

  1. package cn.test.design.domain.service.engine.impl;
  2. import java.util.Map;
  3. import cn.test.design.domain.model.aggregates.TreeRich;
  4. import cn.test.design.domain.model.vo.EngineResult;
  5. import cn.test.design.domain.model.vo.TreeNode;
  6. import cn.test.design.domain.service.engine.EngineBase;
  7. /**
  8. * 基础决策引擎实现
  9. *
  10. */
  11. public class TreeEngineHandle extends EngineBase {
  12. @Override
  13. public EngineResult process(Long treeId, String userId, TreeRich treeRich, Map<String, String> decisionMatter) {
  14. // 决策流程
  15. TreeNode treeNode = engineDecisionMaker(treeRich, treeId, userId, decisionMatter);
  16. // 决策结果
  17. return new EngineResult(userId, treeId, treeNode.getTreeNodeId(), treeNode.getNodeValue());
  18. }
  19. }

4.2.8 单元测试

        这里的初始化配置目前均为手动配置,实际代码可以使用数据库配置动态配置所需要的节点信息。

  1. package cn.test.design.test;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import com.alibaba.fastjson.JSON;
  11. import cn.test.design.domain.model.aggregates.TreeRich;
  12. import cn.test.design.domain.model.vo.EngineResult;
  13. import cn.test.design.domain.model.vo.TreeNode;
  14. import cn.test.design.domain.model.vo.TreeNodeLink;
  15. import cn.test.design.domain.model.vo.TreeRoot;
  16. import cn.test.design.domain.service.engine.IEngine;
  17. import cn.test.design.domain.service.engine.impl.TreeEngineHandle;
  18. public class ApiTest {
  19. private Logger logger = LoggerFactory.getLogger(ApiTest.class);
  20. private TreeRich treeRich;
  21. @Before
  22. public void init() {
  23. // 节点:1
  24. TreeNode treeNode_01 = new TreeNode();
  25. treeNode_01.setTreeId(10001L);
  26. treeNode_01.setTreeNodeId(1L);
  27. treeNode_01.setNodeType(1);
  28. treeNode_01.setNodeValue(null);
  29. treeNode_01.setRuleKey("channel");
  30. treeNode_01.setRuleDesc("渠道");
  31. // 链接:1->11
  32. TreeNodeLink treeNodeLink_11 = new TreeNodeLink();
  33. treeNodeLink_11.setNodeIdFrom(1L);
  34. treeNodeLink_11.setNodeIdTo(11L);
  35. treeNodeLink_11.setRuleLimitType(1);
  36. treeNodeLink_11.setRuleLimitValue("0001");
  37. // 链接:1->12
  38. TreeNodeLink treeNodeLink_12 = new TreeNodeLink();
  39. treeNodeLink_12.setNodeIdTo(1L);
  40. treeNodeLink_12.setNodeIdTo(12L);
  41. treeNodeLink_12.setRuleLimitType(1);
  42. treeNodeLink_12.setRuleLimitValue("0002");
  43. List<TreeNodeLink> treeNodeLinkList_1 = new ArrayList<>();
  44. treeNodeLinkList_1.add(treeNodeLink_11);
  45. treeNodeLinkList_1.add(treeNodeLink_12);
  46. treeNode_01.setTreeNodeLinkList(treeNodeLinkList_1);
  47. // 节点:11
  48. TreeNode treeNode_11 = new TreeNode();
  49. treeNode_11.setTreeId(10001L);
  50. treeNode_11.setTreeNodeId(11L);
  51. treeNode_11.setNodeType(1);
  52. treeNode_11.setNodeValue(null);
  53. treeNode_11.setRuleKey("userSalesVolume");
  54. treeNode_11.setRuleDesc("销售额A");
  55. // 链接:11->111
  56. TreeNodeLink treeNodeLink_111 = new TreeNodeLink();
  57. treeNodeLink_111.setNodeIdFrom(11L);
  58. treeNodeLink_111.setNodeIdTo(111L);
  59. treeNodeLink_111.setRuleLimitType(3);
  60. treeNodeLink_111.setRuleLimitValue("25");
  61. // 链接:11->112
  62. TreeNodeLink treeNodeLink_112 = new TreeNodeLink();
  63. treeNodeLink_112.setNodeIdFrom(11L);
  64. treeNodeLink_112.setNodeIdTo(112L);
  65. treeNodeLink_112.setRuleLimitType(5);
  66. treeNodeLink_112.setRuleLimitValue("25");
  67. List<TreeNodeLink> treeNodeLinkList_11 = new ArrayList<>();
  68. treeNodeLinkList_11.add(treeNodeLink_111);
  69. treeNodeLinkList_11.add(treeNodeLink_112);
  70. treeNode_11.setTreeNodeLinkList(treeNodeLinkList_11);
  71. // 节点:12
  72. TreeNode treeNode_12 = new TreeNode();
  73. treeNode_12.setTreeId(10001L);
  74. treeNode_12.setTreeNodeId(12L);
  75. treeNode_12.setNodeType(1);
  76. treeNode_12.setNodeValue(null);
  77. treeNode_12.setRuleKey("userSalesVolume");
  78. treeNode_12.setRuleDesc("销售额B");
  79. // 链接:12->121
  80. TreeNodeLink treeNodeLink_121 = new TreeNodeLink();
  81. treeNodeLink_121.setNodeIdFrom(12L);
  82. treeNodeLink_121.setNodeIdTo(121L);
  83. treeNodeLink_121.setRuleLimitType(3);
  84. treeNodeLink_121.setRuleLimitValue("25");
  85. // 链接:12->122
  86. TreeNodeLink treeNodeLink_122 = new TreeNodeLink();
  87. treeNodeLink_122.setNodeIdFrom(12L);
  88. treeNodeLink_122.setNodeIdTo(122L);
  89. treeNodeLink_122.setRuleLimitType(5);
  90. treeNodeLink_122.setRuleLimitValue("25");
  91. List<TreeNodeLink> treeNodeLinkList_12 = new ArrayList<>();
  92. treeNodeLinkList_12.add(treeNodeLink_121);
  93. treeNodeLinkList_12.add(treeNodeLink_122);
  94. treeNode_12.setTreeNodeLinkList(treeNodeLinkList_12);
  95. // 节点:111
  96. TreeNode treeNode_111 = new TreeNode();
  97. treeNode_111.setTreeId(10001L);
  98. treeNode_111.setTreeNodeId(111L);
  99. treeNode_111.setNodeType(2);
  100. treeNode_111.setNodeValue("果实A");
  101. // 节点:112
  102. TreeNode treeNode_112 = new TreeNode();
  103. treeNode_112.setTreeId(10001L);
  104. treeNode_112.setTreeNodeId(112L);
  105. treeNode_112.setNodeType(2);
  106. treeNode_112.setNodeValue("果实B");
  107. // 节点:121
  108. TreeNode treeNode_121 = new TreeNode();
  109. treeNode_121.setTreeId(10001L);
  110. treeNode_121.setTreeNodeId(121L);
  111. treeNode_121.setNodeType(2);
  112. treeNode_121.setNodeValue("果实C");
  113. // 节点:122
  114. TreeNode treeNode_122 = new TreeNode();
  115. treeNode_122.setTreeId(10001L);
  116. treeNode_122.setTreeNodeId(122L);
  117. treeNode_122.setNodeType(2);
  118. treeNode_122.setNodeValue("果实D");
  119. // 树根
  120. TreeRoot treeRoot = new TreeRoot();
  121. treeRoot.setTreeId(10001L);
  122. treeRoot.setTreeRootNodeId(1L);
  123. treeRoot.setTreeName("规则决策树");
  124. Map<Long, TreeNode> treeNodeMap = new HashMap<>();
  125. treeNodeMap.put(1L, treeNode_01);
  126. treeNodeMap.put(11L, treeNode_11);
  127. treeNodeMap.put(12L, treeNode_12);
  128. treeNodeMap.put(111L, treeNode_111);
  129. treeNodeMap.put(112L, treeNode_112);
  130. treeNodeMap.put(121L, treeNode_121);
  131. treeNodeMap.put(122L, treeNode_122);
  132. treeRich = new TreeRich(treeRoot, treeNodeMap);
  133. }
  134. @Test
  135. public void test_tree() {
  136. logger.info("决策树组合结构信息:\r\n" + JSON.toJSONString(treeRich));
  137. IEngine treeEngineHandle = new TreeEngineHandle();
  138. /**
  139. * 测试数据
  140. * 果实A:gender=man、age=22
  141. * 果实B:gender=man、age=29
  142. * 果实C:gender=woman、age=22
  143. * 果实D:gender=woman、age=29
  144. */
  145. Map<String, String> decisionMatter = new HashMap<>();
  146. decisionMatter.put("channel", "0001");
  147. decisionMatter.put("userSalesVolume", "29");
  148. EngineResult result = treeEngineHandle.process(10001L, "Oli09pLkdjh", treeRich, decisionMatter);
  149. logger.info("测试结果:{}", JSON.toJSONString(result));
  150. }
  151. @Test
  152. public void t() {
  153. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01000011", 2)).trim()));
  154. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01101111", 2)).trim()));
  155. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01100100", 2)).trim()));
  156. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01101001", 2)).trim()));
  157. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01101110", 2)).trim()));
  158. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01100111", 2)).trim()));
  159. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01001000", 2)).trim()));
  160. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01100101", 2)).trim()));
  161. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01101100", 2)).trim()));
  162. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01110000", 2)).trim()));
  163. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01110011", 2)).trim()));
  164. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01001100", 2)).trim()));
  165. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01101001", 2)).trim()));
  166. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01100110", 2)).trim()));
  167. System.out.println(hexStringToString(String.format("%21X", Long.parseLong("01100101", 2)).trim()));
  168. }
  169. public static String hexStringToString(String s) {
  170. if (s == null || s.equals("")) {
  171. return null;
  172. }
  173. s = s.replace(" ", "");
  174. byte[] baKeyword = new byte[s.length() / 2];
  175. for (int i = 0; i < baKeyword.length; i++) {
  176. try {
  177. baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
  178. } catch (Exception e) {
  179. e.printStackTrace();
  180. }
  181. }
  182. try {
  183. s = new String(baKeyword, "UTF-8");
  184. new String();
  185. } catch (Exception e1) {
  186. e1.printStackTrace();
  187. }
  188. return s;
  189. }
  190. }

4.2.9 相关基础节点类

        这里写下相关基础类

cn.test.design.domain.model.aggregatesTreeRich

规则树聚合

组织树各节点(叶子,果实)

cn.test.design.domain.model.voEngineResult决策结果
cn.test.design.domain.model.voTreeNode规则树节点信息
cn.test.design.domain.model.voTreeNodeLink规则树线信息 链接叶子节点和果实节点
cn.test.design.domain.model.voTreeRoot树根信息

规则树聚合TreeRich

  1. package cn.test.design.domain.model.aggregates;
  2. import java.util.Map;
  3. import cn.test.design.domain.model.vo.TreeNode;
  4. import cn.test.design.domain.model.vo.TreeRoot;
  5. /**
  6. * 规则树聚合
  7. */
  8. public class TreeRich {
  9. // 树根信息
  10. private TreeRoot treeRoot;
  11. // 树节点ID -> 子节点
  12. private Map<Long, TreeNode> treeNodeMap;
  13. public TreeRich(TreeRoot treeRoot, Map<Long, TreeNode> treeNodeMap) {
  14. this.treeRoot = treeRoot;
  15. this.treeNodeMap = treeNodeMap;
  16. }
  17. public TreeRoot getTreeRoot() {
  18. return treeRoot;
  19. }
  20. public void setTreeRoot(TreeRoot treeRoot) {
  21. this.treeRoot = treeRoot;
  22. }
  23. public Map<Long, TreeNode> getTreeNodeMap() {
  24. return treeNodeMap;
  25. }
  26. public void setTreeNodeMap(Map<Long, TreeNode> treeNodeMap) {
  27. this.treeNodeMap = treeNodeMap;
  28. }
  29. }

决策结果EngineResult

  1. package cn.test.design.domain.model.vo;
  2. /**
  3. * 决策结果
  4. */
  5. public class EngineResult {
  6. // 执行结果
  7. private boolean isSuccess;
  8. // 用户ID
  9. private String userId;
  10. // 规则树ID
  11. private Long treeId;
  12. // 果实节点ID
  13. private Long nodeId;
  14. // 果实节点值
  15. private String nodeValue;
  16. public EngineResult() {
  17. }
  18. public EngineResult(boolean isSuccess) {
  19. this.isSuccess = isSuccess;
  20. }
  21. public EngineResult(String userId, Long treeId, Long nodeId, String nodeValue) {
  22. this.isSuccess = true;
  23. this.userId = userId;
  24. this.treeId = treeId;
  25. this.nodeId = nodeId;
  26. this.nodeValue = nodeValue;
  27. }
  28. public boolean isSuccess() {
  29. return isSuccess;
  30. }
  31. public void setSuccess(boolean success) {
  32. isSuccess = success;
  33. }
  34. public String getUserId() {
  35. return userId;
  36. }
  37. public void setUserId(String userId) {
  38. this.userId = userId;
  39. }
  40. public Long getTreeId() {
  41. return treeId;
  42. }
  43. public void setTreeId(Long treeId) {
  44. this.treeId = treeId;
  45. }
  46. public Long getNodeId() {
  47. return nodeId;
  48. }
  49. public void setNodeId(Long nodeId) {
  50. this.nodeId = nodeId;
  51. }
  52. public String getNodeValue() {
  53. return nodeValue;
  54. }
  55. public void setNodeValue(String nodeValue) {
  56. this.nodeValue = nodeValue;
  57. }
  58. }

规则树节点信息 TreeNode

  1. package cn.test.design.domain.model.vo;
  2. import java.util.List;
  3. /**
  4. * 规则树节点信息
  5. */
  6. public class TreeNode {
  7. // 规则树ID
  8. private Long treeId;
  9. // 规则树节点ID
  10. private Long treeNodeId;
  11. // 节点类型;1叶子、2 果实
  12. private Integer nodeType;
  13. // 节点值[nodeType=2];果实值
  14. private String nodeValue;
  15. // 规则Key
  16. private String ruleKey;
  17. // 规则描述
  18. private String ruleDesc;
  19. // 节点链路
  20. private List<TreeNodeLink> treeNodeLinkList;
  21. public Long getTreeId() {
  22. return treeId;
  23. }
  24. public void setTreeId(Long treeId) {
  25. this.treeId = treeId;
  26. }
  27. public Long getTreeNodeId() {
  28. return treeNodeId;
  29. }
  30. public void setTreeNodeId(Long treeNodeId) {
  31. this.treeNodeId = treeNodeId;
  32. }
  33. public Integer getNodeType() {
  34. return nodeType;
  35. }
  36. public void setNodeType(Integer nodeType) {
  37. this.nodeType = nodeType;
  38. }
  39. public String getNodeValue() {
  40. return nodeValue;
  41. }
  42. public void setNodeValue(String nodeValue) {
  43. this.nodeValue = nodeValue;
  44. }
  45. public String getRuleKey() {
  46. return ruleKey;
  47. }
  48. public void setRuleKey(String ruleKey) {
  49. this.ruleKey = ruleKey;
  50. }
  51. public String getRuleDesc() {
  52. return ruleDesc;
  53. }
  54. public void setRuleDesc(String ruleDesc) {
  55. this.ruleDesc = ruleDesc;
  56. }
  57. public List<TreeNodeLink> getTreeNodeLinkList() {
  58. return treeNodeLinkList;
  59. }
  60. public void setTreeNodeLinkList(List<TreeNodeLink> treeNodeLinkList) {
  61. this.treeNodeLinkList = treeNodeLinkList;
  62. }
  63. }

规则树线信息 链接叶子节点和果实节点 TreeNodeLink

  1. package cn.test.design.domain.model.vo;
  2. /**
  3. * 规则树线信息 链接叶子节点和果实节点
  4. */
  5. public class TreeNodeLink {
  6. // 节点From
  7. private Long nodeIdFrom;
  8. // 节点To
  9. private Long nodeIdTo;
  10. // 限定类型;1:=;2:>;3:<;4:>=;5<=;6:enum[枚举范围]
  11. private Integer ruleLimitType;
  12. // 限定值
  13. private String ruleLimitValue;
  14. public Long getNodeIdFrom() {
  15. return nodeIdFrom;
  16. }
  17. public void setNodeIdFrom(Long nodeIdFrom) {
  18. this.nodeIdFrom = nodeIdFrom;
  19. }
  20. public Long getNodeIdTo() {
  21. return nodeIdTo;
  22. }
  23. public void setNodeIdTo(Long nodeIdTo) {
  24. this.nodeIdTo = nodeIdTo;
  25. }
  26. public Integer getRuleLimitType() {
  27. return ruleLimitType;
  28. }
  29. public void setRuleLimitType(Integer ruleLimitType) {
  30. this.ruleLimitType = ruleLimitType;
  31. }
  32. public String getRuleLimitValue() {
  33. return ruleLimitValue;
  34. }
  35. public void setRuleLimitValue(String ruleLimitValue) {
  36. this.ruleLimitValue = ruleLimitValue;
  37. }
  38. }

树根信息 TreeRoot

  1. package cn.test.design.domain.model.vo;
  2. /**
  3. * 树根信息
  4. */
  5. public class TreeRoot {
  6. // 规则树ID
  7. private Long treeId;
  8. // 规则树根ID
  9. private Long treeRootNodeId;
  10. // 规则树名称
  11. private String treeName;
  12. public Long getTreeId() {
  13. return treeId;
  14. }
  15. public void setTreeId(Long treeId) {
  16. this.treeId = treeId;
  17. }
  18. public Long getTreeRootNodeId() {
  19. return treeRootNodeId;
  20. }
  21. public void setTreeRootNodeId(Long treeRootNodeId) {
  22. this.treeRootNodeId = treeRootNodeId;
  23. }
  24. public String getTreeName() {
  25. return treeName;
  26. }
  27. public void setTreeName(String treeName) {
  28. this.treeName = treeName;
  29. }
  30. }

五、扬帆起航

        写在最后是不是发现这种模式就像电脑主机一样不同的接口是可以组合拔插的也可以随时配置不同的配置,这种模式保证了开闭原则只要前期定义好对应的结构,每次新增诉求只需要新增对应的节点即可,或者都可以做为运营后台模式由运营动态配置对应的路由规则便于后续维护。

本文参考:

《重学java设计模式》-付政委。

  百度百科。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/1022401
推荐阅读
相关标签
  

闽ICP备14008679号