gpt4 book ai didi

naming-conventions - 接口(interface)和版本控制

转载 作者:行者123 更新时间:2023-12-04 01:46:12 25 4
gpt4 key购买 nike

我正在设计一个新系统,并且我有很多接口(interface)会随着系统的推移而增长。命名此接口(interface)的最佳做法是什么

ISomethingV01
ISomethingV02
etc

我这样做
public interface ISomething{
void method();
}

那么我必须添加方法2所以现在我该怎么办?
public interface ISomethingV2:ISomething{
void method2();
}

还是其他方式?

最佳答案

我认为您正在过度使用接口(interface)。

Meyer 和 Martin 告诉我们:“对扩展开放,对修改关闭!”

然后 Cwalina (et al) 重申:

从框架设计指南...

In general, classes are the preferred construct for exposing abstractions. The main drawback of interfaces is that they are much less flexible than classes when it comes to allowing for evolution of APIs. Once you ship an interface, the set of its members is fixed forever. Any additions to the interface would break existing types implementing the interface.

A class offers much more flexibility. You can add members to classes that have already shipped. As long as the method is not abstract (i.e., as long as you provide a default implementation of the method), any existing derived classes continue to function unchanged.



alt text

关于naming-conventions - 接口(interface)和版本控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45123/

25 4 0