知识卡片
运算闭包保留同类结果
运算闭包(Closure of Operations)指某类对象参与操作后,结果仍属于同一概念类型。例如两个实数相加仍得实数,两个 Pigment Color 经 mixedWith() 得到新的 Pigment Color。当操作以同一类型接收参数并返回结果时,调用者无需引入额外领域概念,也容易把结果继续作为下一次输入。
这尤其适合用于计算的值对象。实体的身份和生命周期有领域意义,通常不能为回答一次计算随意造出新实体;但“Employee 返回其 supervisor,仍为 Employee”是原文给出的实体闭包例外。闭包可基于抽象类型,具体子类型不必相同。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 原书第10章|Closure of Operations · 查看原书依据
Where it fits, define an operation whose return type is the same as the type of its argument(s). If the implementer has state that is used in the computation, then the implementer is effectively an argument of the operation, so the argument(s) and return value should be of the same type as the implementer.
参考来源
- 位置:《Domain-Driven Design: Tackling Complexity in the Heart of Software》第78章《Closure of Operations》(源文件:_epub-src/0321125215_ch10lev1sec6.html;单元01)
- 结论依据:原文定义同类型参数与结果的闭包接口,以实数、颜料颜色说明组合性;并说明值对象通常更适合,实体有生命周期限制但并非绝无闭包。
- 原始内容:Where it fits, define an operation whose return type is the same as the type of its argument(s). If the implementer has state that is used in the computation, then the implementer is effectively an argument of the operation, so the argument(s) and return value should be of the same type as the implementer.