通常建议“针对接口(interface)而不是实现编程”。它有助于促进关注点分离,并有助于单元测试。但是,我在考虑 API 编程。
假设我编写了一个 API,该 API 使用了大量“接口(interface)编程”。我们还可以说该 API 非常流行并被许多外部客户使用。如果必须更改 API 中的接口(interface)之一,则需要重新编译使用该 API 的应用。
我的问题是,如何避免此类问题(或减少此类更改的影响),还是无法避免?我不是 API 程序员,想知道这里的最佳实践。在我看来,改变一个已经存在很长时间并被广泛使用的接口(interface)是个坏主意。
请您参考如下方法:
已发布的界面不应该改变。如果您必须增加功能,只需添加一个新界面即可。
引自MSDN :
When you create an interface, you are creating a definition that can never change after the interface definition has been released. This interface invariance is an important principle of component design, because it protects existing systems that have been written to use the interface.
When an interface is clearly in need of enhancement, a new interface should be created. This interface might be named by appending a "2" onto the original interface name, to show its relationship to the existing interface.
