知识卡片
可变值对象不得共享
值对象默认按不可变设计,以便安全复制、传递和共享。若值频繁变化、创建或替换成本高,且确实需要性能调优,可例外采用可变实现;此时不得共享该实例,否则一处修改会影响其他持有者。是否允许可变取决于具体技术环境和共享方式,模型仍需保留值对象的业务含义。参见[[不可变使值对象安全共享]]。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 原书第05章|Special Cases: When to Allow Mutability · 查看原书依据
If a VALUE's implementation is to be mutable, then it must not be shared. Whether you will be sharing or not, design VALUE OBJECTS as immutable when you can.
参考来源
- 位置:《Domain-Driven Design: Tackling Complexity in the Heart of Software》第39章《原书第05章|Special Cases: When to Allow Mutability》(源文件:_epub-src/0321125215_ch05lev1sec3_split_002.html)
- 结论依据:原文先给出频繁变化、对象创建与替换成本等可变实现的性能因素,随后明确要求可变值对象不能共享,并重申能不可变时仍优先不可变。
- 原始内容:Even so, there are cases when performance considerations will favor allowing a VALUE OBJECT to be mutable. ... If a VALUE 's implementation is to be mutable, then it must not be shared. Whether you will be sharing or not, design VALUE OBJECTS as immutable when you can.