知识卡片
份额饼值对象使运算可组合
把 Share Pie 改为不可变值对象后,操作不修改旧饼,而返回新饼:plus(otherPie) 合并、minus(otherPie) 相减,属于同类结果的运算闭包;prorated(amount) 按比例生成新饼,金额参数使它不是严格同类型闭包。
原书图10.20示意 Loan 只需持有 Pie 并在交易命令中替换值,复杂计算留在 Pie。于是中间试算可自由组合而不改变贷款状态。图示类图表达职责和返回类型,具体舍入、负值、总额为零等金融边界原文未在此验证。参见[[运算闭包保留同类结果]]。
图10.20 将 SharePie.getAmount() 的返回类型标为 Money,而相邻示例代码写作 public double getAmount()。下图保留原图类型;两处原书证据不一致,不能据此推定实际代码采用 Money。
结构图:
classDiagram
class Loan {
-shares : SharePie
+applyPrincipalPayment(paymentShares)
}
class SharePie {
+plus(otherPie) SharePie
+minus(otherPie) SharePie
+prorated(amount) SharePie
+getAmount() Money
}
class Share {
+owner
+amount
}
Loan --> SharePie : 持有并替换
SharePie o-- Share : 组成
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 原书第10章|Angles of Attack · 查看原书依据
Often, the hands-on experience of implementing a new design will trigger a new insight into the model itself. In this case, the tight coupling of the Loan and Share Pie seems to be obscuring the relationship of the Share Pie and the Shares. What would happen if we made Share Pie a VALUE OBJECT?
参考来源
- 位置:《Domain-Driven Design: Tackling Complexity in the Heart of Software》第82章《Angles of Attack》(源文件:_epub-src/0321125215_ch10lev1sec9.html;单元03)
- 结论依据:原文先说明不可变 Pie 需整体替换,再把加减定义为返回新 Pie 的同类运算;图10.20展示方法与 Loan 的持有关系。原图把 `getAmount()` 返回类型标为 `Money`,相邻示例代码却写为 `double`。
- 原始内容:Often, the hands-on experience of implementing a new design will trigger a new insight into the model itself. In this case, the tight coupling of the Loan and Share Pie seems to be obscuring the relationship of the Share Pie and the Shares . What would happen if we made Share Pie a VALUE OBJECT ?