知识卡片
组合模式先确认同类整体部分
递归遍历方便,并不足以证明该用 Composite。先确认领域里存在真实的整体—部分层级,且不同层的整体和部分能归入同一种概念;若各层在业务上本来不同,强行统一接口会掩盖意义。
成立时可定义共同抽象:叶节点用自身值回答问题,容器节点把子节点结果聚合。客户端在各层提出相同的领域问题,而无须知道当前对象是叶还是容器。关键是行为对称,不只是画出树形类图。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 原书第12章|Composite · 查看原书依据
When applying any design pattern in the domain, the first concern should be whether the pattern idea really is a good fit for the domain concept. It might be convenient to move recursively through some associated objects, but is there a true whole-part hierarchy? Have you found an abstraction under which all the parts truly are the same conceptual type?
参考来源
- 位置:《Domain-Driven Design: Tackling Complexity in the Heart of Software》第89章《Composite》(源文件:_epub-src/0321125215_ch12lev1sec2.html;单元01)
- 结论依据:原文要求先验证真实整体-部分关系与同类抽象,再规定叶和容器共享信息操作;行为对称是模式力量。
- 原始内容:When applying any design pattern in the domain, the first concern should be whether the pattern idea really is a good fit for the domain concept. It might be convenient to move recursively through some associated objects, but is there a true whole-part hierarchy? Have you found an abstraction under which all the parts truly are the same conceptual type?