知识卡片
公共可见性扩大库迁移面
库内部类若普遍公开,消费者可直接依赖本无意承诺稳定的实现细节,后续重构就会变成跨项目迁移。设计库时应限制内部对象的可见性,并明确支持的公共接口。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
原书出处
参考来源
- 位置:《Principles of Software Architecture Modernization》第4章《Anti-Patterns: Internal Shared Libraries》(源文件:`_epub-src/OPS/ch04.xhtml`,归一化正文 [4030:8221))
- 结论依据:原文说明消费者容易直接使用库内部实现,并指出内部库常把所有类设为公开,枚举、注解等都可能被依赖。
- 原始内容:Use internal implementations: Directly use internal objects and functions that are not intended by the library’s authors to be shared. Implement interfaces: Implement interfaces and/or extend classes that should not be allowed to be extended. Java has some solutions we can use to reduce these mistakes. It’s possible to use final and package levels of visibility in classes to protect the internal implementation of libraries.