定义
组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。
这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。
优点:
- 高层模块调用简单。
- 节点自由增加。
缺点:在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。
使用场景:部分、整体场景,如树形菜单,文件、文件夹的管理。
注意事项:定义时为具体类。
实践
决策树场景模拟
下图为一个简化版的营销规则决策树,根据性别、年龄的不同组合,发放不同类型的优惠券,目的是刺激消费,对精准用户进行促活。
虽然我们可能没有开发过营销项目,但可能时时刻刻都在被营销着。例如,男生喜欢经常浏览机械键盘、笔记本电脑和汽车装饰等,商家会推荐此类商品的信息或优惠通知。女生喜欢浏览衣服、化妆品和箱包等,商家会推荐此类商品的信息或促销消息。虽然用户进入的是同一款软件,但最终展示的内容会略有不同,这些都是营销的案例。对于不常使用电商软件的用户,商家可能还会稍微加大折扣的力度,来增加用户黏性。
这里模拟一个类似的决策场景,体现组合模式在其中起到的作用。另外,组合模式不仅可以运用于规则决策树,还可以做服务包装,将不同的接口进行组合配置,对外提供服务能力,降低开发成本。
违背设计模式实现
这种情况一般是使用很多的 if...else 进行实现,把判断逻辑使用 if…else 写到一个类中。使用面向过程的优点是代码实现得快,但缺点也很多:不好维护和扩展,出了问题难以排查,新加功能的风险较高等。
详情点击 链接 进行查看
组合设计模式实现
整个代码结构如下图所示:
整个类图关系包括了树形结构原子模块实现关系、树形结构执行引擎两部分内容。树形结构原子模块实现关系从 LogicFilter 开始定义适配的决策过滤器,BaseLogic 是对接口的实现,以提供最基本的通用方法。UserAgeFilter 和 UserGenerFilter 是两个具体的实现类,用于判断年龄和性别。树形结构执行引擎是对这棵可以被组织出来的决策树进行执行的引擎,同样定义了引擎接口和基础的配置,在配置里面设定了需要的模式决策节点。 另外,在类图中插入了一个树形结构关系模拟树形结构,由树的7个节点 1、11、12、111、112、121、122 左右串联,组合出一棵二叉关系树。
决策树模型:
model 包下的类:
model包下的对象用于描述决策树的各项信息类,包括:聚合对象、决策结果、树节点、树节点链路关系和树根信息。
代码语言:javascript复制@Data
public class TreeRoot {
private Long treeId;
private Long treeRootNodeId;
private String treeName;
}
@Data
public class TreeNodeLink {
private Long nodeIdFrom; //节点From
private Long nodeIdTo; //节点To
private Integer ruleLimitType; //限定类型;1:=;2:>;3:<;4:>=;5<=;6:enum[枚举范围]
private String ruleLimitValue; //限定值
}
@Data
public class TreeNode {
private Long treeId; //规则树ID
private Long treeNodeId; //规则树节点ID
private Integer nodeType; //节点类型;1子叶、2果实
private String nodeValue; //节点值[nodeType=2];果实值
private String ruleKey; //规则Key
private String ruleDesc; //规则描述
private List<TreeNodeLink> treeNodeLinkList; //节点链路
}
@Data
public class EngineResult {
private boolean isSuccess; //执行结果
private String userId; //用户ID
private Long treeId; //规则树ID
private Long nodeId; //果实节点ID
private String nodeValue;//果实节点值
public EngineResult() {
}
public EngineResult(boolean isSuccess) {
this.isSuccess = isSuccess;
}
public EngineResult(String userId, Long treeId, Long nodeId, String nodeValue) {
this.isSuccess = true;
this.userId = userId;
this.treeId = treeId;
this.nodeId = nodeId;
this.nodeValue = nodeValue;
}
}
@Data
public class TreeRich {
private TreeRoot treeRoot;
private Map<Long, TreeNode> treeNodeMap;
public TreeRich(TreeRoot treeRoot, Map<Long, TreeNode> treeNodeMap) {
this.treeRoot = treeRoot;
this.treeNodeMap = treeNodeMap;
}
}
树节点逻辑过滤器接口
代码语言:javascript复制public interface LogicFilter {
/**
* 逻辑决策器
*
* @param matterValue 决策值
* @param treeNodeLineInfoList 决策节点
* @return 下一个节点Id
*/
Long filter(String matterValue, List<TreeNodeLink> treeNodeLineInfoList);
/**
* 获取决策值
*
* @param decisionMatter 决策物料
* @return 决策值
*/
String matterValue(Long treeId, String userId, Map<String, String> decisionMatter);
}
这部分定义了适配的通用接口和相应的方法:逻辑决策器方法、获取决策值方法,让每一个提供决策能力的节点都必须实现此接口,保证统一性。
决策抽象类提供基础服务
代码语言:javascript复制public abstract class BaseLogic implements LogicFilter {
@Override
public Long filter(String matterValue, List<TreeNodeLink> treeNodeLinkList) {
for (TreeNodeLink nodeLine : treeNodeLinkList) {
if (decisionLogic(matterValue, nodeLine)) {
return nodeLine.getNodeIdTo();
}
}
return 0L;
}
@Override
public abstract String matterValue(Long treeId, String userId, Map<String, String> decisionMatter);
private boolean decisionLogic(String matterValue, TreeNodeLink nodeLink) {
switch (nodeLink.getRuleLimitType()) {
case 1:
return matterValue.equals(nodeLink.getRuleLimitValue());
case 2:
return Double.parseDouble(matterValue) > Double.parseDouble(nodeLink.getRuleLimitValue());
case 3:
return Double.parseDouble(matterValue) < Double.parseDouble(nodeLink.getRuleLimitValue());
case 4:
return Double.parseDouble(matterValue) >= Double.parseDouble(nodeLink.getRuleLimitValue());
case 5:
return Double.parseDouble(matterValue) <= Double.parseDouble(nodeLink.getRuleLimitValue());
default:
return false;
}
}
}
在抽象方法中实现了接口方法,同时定义了基本的决策方法:1、2、3、4、5,等于、小于、大于、小于或等于、大于或等于的判断逻辑。同时定义了抽象方法,让每一个实现接口的类都必须按照规则提供决策值,这个决策值用于进行逻辑比对。
树节点逻辑实现类
代码语言:javascript复制// 年龄节点
public class UserAgeFilter extends BaseLogic {
@Override
public String matterValue(Long treeId, String userId, Map<String, String> decisionMatter) {
return decisionMatter.get("age");
}
}
// 性别节点
public class UserGenderFilter extends BaseLogic {
@Override
public String matterValue(Long treeId, String userId, Map<String, String> decisionMatter) {
return decisionMatter.get("gender");
}
}
决策引擎接口定义
代码语言:javascript复制public interface IEngineService {
EngineResult process(final Long treeId, final String userId, TreeRich treeRich, final Map<String, String> decisionMatter);
}
决策节点配置
代码语言:javascript复制@Data
public class EngineConfig {
static Map<String, LogicFilter> logicFilterMap;
static {
logicFilterMap = new ConcurrentHashMap<>();
logicFilterMap.put("userAge", new UserAgeFilter());
logicFilterMap.put("userGender", new UserGenderFilter());
}
}
基础决策引擎功能
代码语言:javascript复制@Slf4j
public abstract class EngineBase extends EngineConfig implements IEngineService {
@Override
public abstract EngineResult process(Long treeId, String userId, TreeRich treeRich, Map<String, String> decisionMatter);
protected TreeNode engineDecisionMaker(TreeRich treeRich, Long treeId, String userId, Map<String, String> decisionMatter) {
TreeRoot treeRoot = treeRich.getTreeRoot();
Map<Long, TreeNode> treeNodeMap = treeRich.getTreeNodeMap();
// 规则树根ID
Long rootNodeId = treeRoot.getTreeRootNodeId();
TreeNode treeNodeInfo = treeNodeMap.get(rootNodeId);
//节点类型[NodeType];1子叶、2果实
while (treeNodeInfo.getNodeType().equals(1)) {
String ruleKey = treeNodeInfo.getRuleKey();
LogicFilter logicFilter = logicFilterMap.get(ruleKey);
String matterValue = logicFilter.matterValue(treeId, userId, decisionMatter);
Long nextNode = logicFilter.filter(matterValue, treeNodeInfo.getTreeNodeLinkList());
treeNodeInfo = treeNodeMap.get(nextNode);
log.info("决策树引擎=>{} userId:{} treeId:{} treeNode:{} ruleKey:{} matterValue:{}", treeRoot.getTreeName(), userId, treeId, treeNodeInfo.getTreeNodeId(), ruleKey, matterValue);
}
return treeNodeInfo;
}
}
这里主要提供决策树流程的处理过程,有点像通过链路的关系(性别、年龄)在二叉树中寻找果实节点的过程。同时提供一个抽象方法,执行决策流程的方法,供外部做具体的实现。
决策引擎的实现
代码语言:javascript复制public class TreeEngineHandle extends EngineBase {
@Override
public EngineResult process(Long treeId, String userId, TreeRich treeRich, Map<String, String> decisionMatter) {
// 决策流程
TreeNode treeNode = engineDecisionMaker(treeRich, treeId, userId, decisionMatter);
// 决策结果
return new EngineResult(userId, treeId, treeNode.getTreeNodeId(), treeNode.getNodeValue());
}
}
通过传递进来的必要信息——决策树信息、决策物料值,做具体的树形结构决策。
测试验证
- 初始化决策树数据
- 单元测试验证
``` @Slf4j public class ApiTest {
private TreeRich treeRich;
// 初始化数据!!! @Before public void init() {
代码语言:javascript复制// 节点:1
TreeNode treeNode_01 = new TreeNode();
treeNode_01.setTreeId(10001L);
treeNode_01.setTreeNodeId(1L);
treeNode_01.setNodeType(1);
treeNode_01.setNodeValue(null);
treeNode_01.setRuleKey("userGender");
treeNode_01.setRuleDesc("用户性别[男/女]");
// 链接:1->11
TreeNodeLink treeNodeLink_11 = new TreeNodeLink();
treeNodeLink_11.setNodeIdFrom(1L);
treeNodeLink_11.setNodeIdTo(11L);
treeNodeLink_11.setRuleLimitType(1);
treeNodeLink_11.setRuleLimitValue("man");
// 链接:1->12
TreeNodeLink treeNodeLink_12 = new TreeNodeLink();
treeNodeLink_12.setNodeIdFrom(1L);
treeNodeLink_12.setNodeIdTo(12L);
treeNodeLink_12.setRuleLimitType(1);
treeNodeLink_12.setRuleLimitValue("woman");
List<TreeNodeLink> treeNodeLinkList_1 = new ArrayList<>();
treeNodeLinkList_1.add(treeNodeLink_11);
treeNodeLinkList_1.add(treeNodeLink_12);
treeNode_01.setTreeNodeLinkList(treeNodeLinkList_1);
// 节点:11
TreeNode treeNode_11 = new TreeNode();
treeNode_11.setTreeId(10001L);
treeNode_11.setTreeNodeId(11L);
treeNode_11.setNodeType(1);
treeNode_11.setNodeValue(null);
treeNode_11.setRuleKey("userAge");
treeNode_11.setRuleDesc("用户年龄");
// 链接:11->111
TreeNodeLink treeNodeLink_111 = new TreeNodeLink();
treeNodeLink_111.setNodeIdFrom(11L);
treeNodeLink_111.setNodeIdTo(111L);
treeNodeLink_111.setRuleLimitType(3);
treeNodeLink_111.setRuleLimitValue("25");
// 链接:11->112
TreeNodeLink treeNodeLink_112 = new TreeNodeLink();
treeNodeLink_112.setNodeIdFrom(11L);
treeNodeLink_112.setNodeIdTo(112L);
treeNodeLink_112.setRuleLimitType(4);
treeNodeLink_112.setRuleLimitValue("25");
List<TreeNodeLink> treeNodeLinkList_11 = new ArrayList<>();
treeNodeLinkList_11.add(treeNodeLink_111);
treeNodeLinkList_11.add(treeNodeLink_112);
treeNode_11.setTreeNodeLinkList(treeNodeLinkList_11);
// 节点:12
TreeNode treeNode_12 = new TreeNode();
treeNode_12.setTreeId(10001L);
treeNode_12.setTreeNodeId(12L);
treeNode_12.setNodeType(1);
treeNode_12.setNodeValue(null);
treeNode_12.setRuleKey("userAge");
treeNode_12.setRuleDesc("用户年龄");
// 链接:12->121
TreeNodeLink treeNodeLink_121 = new TreeNodeLink();
treeNodeLink_121.setNodeIdFrom(12L);
treeNodeLink_121.setNodeIdTo(121L);
treeNodeLink_121.setRuleLimitType(3);
treeNodeLink_121.setRuleLimitValue("25");
// 链接:12->122
TreeNodeLink treeNodeLink_122 = new TreeNodeLink();
treeNodeLink_122.setNodeIdFrom(12L);
treeNodeLink_122.setNodeIdTo(122L);
treeNodeLink_122.setRuleLimitType(4);
treeNodeLink_122.setRuleLimitValue("25");
List<TreeNodeLink> treeNodeLinkList_12 = new ArrayList<>();
treeNodeLinkList_12.add(treeNodeLink_121);
treeNodeLinkList_12.add(treeNodeLink_122);
treeNode_12.setTreeNodeLinkList(treeNodeLinkList_12);
// 节点:111
TreeNode treeNode_111 = new TreeNode();
treeNode_111.setTreeId(10001L);
treeNode_111.setTreeNodeId(111L);
treeNode_111.setNodeType(2);
treeNode_111.setNodeValue("果实A");
// 节点:112
TreeNode treeNode_112 = new TreeNode();
treeNode_112.setTreeId(10001L);
treeNode_112.setTreeNodeId(112L);
treeNode_112.setNodeType(2);
treeNode_112.setNodeValue("果实B");
// 节点:121
TreeNode treeNode_121 = new TreeNode();
treeNode_121.setTreeId(10001L);
treeNode_121.setTreeNodeId(121L);
treeNode_121.setNodeType(2);
treeNode_121.setNodeValue("果实C");
// 节点:122
TreeNode treeNode_122 = new TreeNode();
treeNode_122.setTreeId(10001L);
treeNode_122.setTreeNodeId(122L);
treeNode_122.setNodeType(2);
treeNode_122.setNodeValue("果实D");
// 树根
TreeRoot treeRoot = new TreeRoot();
treeRoot.setTreeId(10001L);
treeRoot.setTreeRootNodeId(1L);
treeRoot.setTreeName("规则决策树");
Map<Long, TreeNode> treeNodeMap = new HashMap<>();
treeNodeMap.put(1L, treeNode_01);
treeNodeMap.put(11L, treeNode_11);
treeNodeMap.put(12L, treeNode_12);
treeNodeMap.put(111L, treeNode_111);
treeNodeMap.put(112L, treeNode_112);
treeNodeMap.put(121L, treeNode_121);
treeNodeMap.put(122L, treeNode_122);
treeRich = new TreeRich(treeRoot, treeNodeMap);
代码语言:javascript复制}
@Test public void test_tree() { log.info("决策树组合结构信息:rn" JSON.toJSONString(treeRich)); IEngineService treeEngineHandle = new TreeEngineHandle(); Map<String, String> decisionMatter = new HashMap<>(); decisionMatter.put("gender", "man"); decisionMatter.put("age", "29"); EngineResult result = treeEngineHandle.process(10001L, "Oli09pLkdjh", treeRich, decisionMatter); log.info("测试结果:{}", JSON.toJSONString(result)); }
}
输出结果:
代码语言:javascript复制16:54:09.775 [main] INFO fun.lixn.design.ApiTest - 决策树组合结构信息: {"treeNodeMap":{112:{"nodeType":2,"nodeValue":"果实B","treeId":10001,"treeNodeId":112},1:{"nodeType":1,"ruleDesc":"用户性别[男/女]","ruleKey":"userGender","treeId":10001,"treeNodeId":1,"treeNodeLinkList":[{"nodeIdFrom":1,"nodeIdTo":11,"ruleLimitType":1,"ruleLimitValue":"man"},{"nodeIdFrom":1,"nodeIdTo":12,"ruleLimitType":1,"ruleLimitValue":"woman"}]},121:{"nodeType":2,"nodeValue":"果实C","treeId":10001,"treeNodeId":121},122:{"nodeType":2,"nodeValue":"果实D","treeId":10001,"treeNodeId":122},11:{"nodeType":1,"ruleDesc":"用户年龄","ruleKey":"userAge","treeId":10001,"treeNodeId":11,"treeNodeLinkList":[{"nodeIdFrom":11,"nodeIdTo":111,"ruleLimitType":3,"ruleLimitValue":"25"},{"nodeIdFrom":11,"nodeIdTo":112,"ruleLimitType":4,"ruleLimitValue":"25"}]},12:{"nodeType":1,"ruleDesc":"用户年龄","ruleKey":"userAge","treeId":10001,"treeNodeId":12,"treeNodeLinkList":[{"nodeIdFrom":12,"nodeIdTo":121,"ruleLimitType":3,"ruleLimitValue":"25"},{"nodeIdFrom":12,"nodeIdTo":122,"ruleLimitType":4,"ruleLimitValue":"25"}]},111:{"nodeType":2,"nodeValue":"果实A","treeId":10001,"treeNodeId":111}},"treeRoot":{"treeId":10001,"treeName":"规则决策树","treeRootNodeId":1}} 16:54:09.779 [main] INFO fun.lixj.design.service.engine.EngineBase - 决策树引擎=>规则决策树 userId:Oli09pLkdjh treeId:10001 treeNode:11 ruleKey:userGender matterValue:man 16:54:09.782 [main] INFO fun.lixj.design.service.engine.EngineBase - 决策树引擎=>规则决策树 userId:Oli09pLkdjh treeId:10001 treeNode:112 ruleKey:userAge matterValue:29 16:54:09.783 [main] INFO fun.lixn.design.ApiTest - 测试结果:{"nodeId":112,"nodeValue":"果实B","success":true,"treeId":10001,"userId":"Oli09pLkdjh"}
```
总结
从以上的决策树场景来看,组合模式主要解决的是在不同结构的组织下,一系列简单逻辑节点或者扩展的复杂逻辑节点对外部的调用仍然可以非常简单。
这种设计模式保证了开闭原则,无须更改模型结构就可以提供新的逻辑节点,并配合组织出新的关系树。
当然,如果是一些功能差异化非常大的接口,则包装起来也会变得比较困难,但也并非不能很好地处理,只不过需要做一些适配和特定的开发。
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links: https://lixj.fun/archives/设计模式-组合模式