知识卡片
颜料颜色计算移入值对象
原书教学例子的 Paint.mixIn 原先一边改变颜料体积,一边执行复杂调色计算。把“颜料色”显化为不可变的 PigmentColor 后,mixedWith(other, ratio) 计算并返回新颜色,原有颜色对象不变;Paint 仍负责混入动作及自身状态更新。这样颜色计算可独立测试、组合,也保留了“颜料混合不同于屏幕RGB光混合”的领域区别。图10.5中传入的第二桶颜料未变化,其业务含义尚不明确,不能把本次重构当作全部混合规则已定。
结构图:
classDiagram
class Paint {
volume
pigmentColor
mixIn(other) void
}
class PigmentColor {
red
yellow
blue
mixedWith(other, ratio) PigmentColor
}
Paint --> PigmentColor : 持有颜色
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 原书第10章|Side -Effect-Free Functions · 查看原书依据
Before we do, note that Pigment Color is a VALUE OBJECT. Therefore, it should be treated as immutable. When we mixed paint, the Paint object itself was changed. It was an ENTITY with an ongoing life story. In contrast, a Pigment Color representing a particular shade of yellow is always exactly that. Instead, mixing will result in a new Pigment Color object representing the new color.
参考来源
- 位置:《Domain-Driven Design: Tackling Complexity in the Heart of Software》第74章《Side-Effect-Free Functions》(源文件:_epub-src/0321125215_ch10lev1sec2.html;图10.4–10.8;单元02)
- 结论依据:原书连续五幅图和代码先呈现修改Paint且计算颜色的混合操作,随后抽出Pigment Color及返回新对象的`mixedWith`,同时指出传入颜料的状态问题留待断言一节。
- 原始内容:Before we do, note that Pigment Color is a VALUE OBJECT. Therefore, it should be treated as immutable. When we mixed paint, the Paint object itself was changed. It was an ENTITY with an ongoing life story. In contrast, a Pigment Color representing a particular shade of yellow is always exactly that. Instead, mixing will result in a new Pigment Color object representing the new color.