知识卡片
将超订规则显式建模
原书货运预订案例中,Cargo 是待订货物,Voyage 是航次。业务允许因临时取消而适度超订;案例规则是“已订货物量+新货物量不超过航次容量的 110%”。若把 1.1 只藏在应用方法的条件判断里,专家难以从设计中认出这是一项可讨论的业务策略。
将判断移入 OverbookingPolicy.isAllowed(cargo, voyage),使“超订策略”成为明确概念。它的价值是让团队能命名、检查和共享规则,并不要求每个小规则都单独建类;此处也尚无切换多套规则的需求。图按原书图 1.8–1.10 和代码整理,展示概念如何从货物、航次关系中显现。参见 [[知识丰富模型不止数据结构]]。
结构图:
classDiagram
class Cargo {
+size
}
class Voyage {
+capacity
+bookedCargoSize()
}
class OverbookingPolicy {
+isAllowed(Cargo cargo, Voyage voyage) boolean
}
Voyage --> "*" Cargo : 预订
OverbookingPolicy ..> Cargo : 读取货物量
OverbookingPolicy ..> Voyage : 检查容量与已订量
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 原书第01章|Knowledge-Rich Design · 查看原书依据
It will be clear to all that overbooking is a distinct policy, and the implementation of that rule is explicit and separate.
参考来源
- 位置:《Domain-Driven Design: Tackling Complexity in the Heart of Software》第14章《原书第01章|Knowledge-Rich Design》(源文件:_epub-src/0321125215_ch01lev1sec4.html;图 1.8–1.10)
- 结论依据:原书先画 Cargo 与 Voyage 的关联及容量、尺寸属性,再把 10% 超订判断藏入应用方法,最后以 Overbooking Policy 类和 `isAllowed` 方法使业务规则显式可见;图中类型及依赖据原图和代码重画。原文明确说该设计不应套到每个细节,且此处尚不需要策略替换。
- 原始内容:Now an important business rule is hidden as a guard clause in an application method. ... The overbooking rule is a policy. ... It will be clear to all that overbooking is a distinct policy, and the implementation of that rule is explicit and separate.