知识卡片
先写客户测试探查接口
从调用者视角设计接口时,可先写一段表达期望用法的测试,即使它暂时不能编译,再按测试重构对象。原书教学例子先用 yellow.paint(blue) 和 getV() 检查混合结果,发现调用代码难读;改成 ourPaint.mixIn(blue)、getVolume() 等名称后,测试更能呈现“把蓝色混入黄色”的意图。测试用于探查接口,再让实现通过;改名本身不证明颜色算法正确,也不说明混合的全部约束。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 原书第10章|Intention-Revealing Interfaces · 查看原书依据
The passing test is the starting point. It is unsatisfying at this point because the code in the test doesn't tell us what it is doing. Let's rewrite the test to reflect the way we would like to use the Paint objects if we were writing a client application. Initially, this test will fail. In fact, it won't even compile. We are writing it to explore the interface design of the Paint object from the client developer's point of view.
参考来源
- 位置:《Domain-Driven Design: Tackling Complexity in the Heart of Software》第73章《Intention-Revealing Interfaces》(源文件:_epub-src/0321125215_ch10lev1sec1.html;图10.2–10.3)
- 结论依据:原书先给出可通过却难懂的颜料测试,再写一段起初不编译、但更接近期望调用语言的测试,随后调整 Paint 类使其通过;两幅类图只显示字段与方法名改变,没有证明算法与完整契约。
- 原始内容:The passing test is the starting point. It is unsatisfying at this point because the code in the test doesn't tell us what it is doing. Let's rewrite the test to reflect the way we would like to use the Paint objects if we were writing a client application. Initially, this test will fail. In fact, it won't even compile. We are writing it to explore the interface design of the Paint object from the client developer's point of view.