此卡片不在当前主题的阅读序列中,可返回主题继续阅读。
知识卡片
约束规则需要显式命名
桶的 contents 不得超过 capacity 是原书教学例子:简单时可在 pourIn 中判断;规则变复杂时,把判断抽成 constrainedToCapacity 等有意图的名称,调用者仍聚焦倒入操作。约束显化不意味着每条规则都要升为独立对象;先看方法能否清楚承载。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 原书第09章|How to Model Less Obvious Kinds of Concepts · 查看原书依据
This very simple rule was understandable in its original form, but when the rules being enforced are more complex, they start to overwhelm the object or operation they apply to, as any implicit concept does.
参考来源
- 位置:《Domain-Driven Design: Tackling Complexity in the Heart of Software》第70章《How to Model Less Obvious Kinds of Concepts》,Explicit Constraints,图9.10(源文件:_epub-src/0321125215_ch09lev1sec2_split_000.html)
- 结论依据:原文用桶的容量不变量说明同一规则可从分支提升为可讨论的命名方法。
- 原始内容:Sometimes constraints find a natural home in an object or method. A "Bucket" object must guarantee the invariant that it does not hold more than its capacity. A simple invariant like this can be enforced using case logic in each operation capable of changing contents. [...] Both versions of this code enforce the constraint, but the second has a more obvious relationship to the model (the basic requirement of MODEL-DRIVEN DESIGN ). This very simple rule was understandable in its original form, but when the rules being enforced are more complex, they start to overwhelm the object or operation they apply to, as any implicit concept does. Factoring the constraint into its own method allows us to give it an intention-revealing name that makes the constraint explicit in our design. It is now a named thing we can discuss. This approach also gives the constraint room. A more complex rule than this might easily produce a method longer than its caller (the pourIn() method, in this case). This way, the caller stays simple and focused on its task while the constraint can grow in complexity if need be.