知识卡片
契约应与实现分离
契约若混在实现类中,内部重构很容易无意改变公开调用面。把必要的公开部分抽到明确接口,能让所有者单独管理契约;但实现更换仍须保留约定的行为、输入输出和数据含义。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
原书出处
参考来源
- 位置:《Principles of Software Architecture Modernization》第2章《Anti-Patterns: Lack of Isolation》(源文件:`_epub-src/OPS/ch02.xhtml`,归一化正文 [46162:50347))
- 结论依据:原文指出混合的契约不显式也不解耦,随后用抽取接口的税务处理例子说明实现可更换但不得破坏消费者对行为与数据类型的预期。
- 原始内容:The contract is not explicit: The contract is mixed with the implementation and thus does not appear to be properly managed, owned and maintained. The contract is not decoupled: Again, the contract is mixed with the implementation. As the implementation changes, the contract can easily break. Ideally, the contract should not break with any implementation changes.