在一个现有接口中添加方法,该接口在大量类中具有不同的实现。

huangapple go评论65阅读模式
英文:

Adding method in an existing interface which has different implementations in large number of classes

问题

如果我们的应用程序中有许多类实现了一个接口,并且我们需要向接口中添加一个方法,推荐的做法是什么?

如果我们使用较旧版本的 Java(比如说1.5),与使用较新版本的 Java(比如说1.8),这种做法是否会有所不同?

英文:

If we have an interface which is being implemented by large number of classes in our application, and we need to add a method to the interface, What is the recommended way to do it?

Would the approach differ if we are using older version of java (let's say 1.5) vs if we are using newer version of java (let's say 1.8).

答案1

得分: 2

这可能不是一个受欢迎的回答,但在我看来,您设想的操作是一个困难的操作。

接口在不同的子系统之间定义了服务契约。更改该契约应该比更改实现更困难。Java 8引入了接口上的“默认”方法,它将接口从严格的契约转变为一种抽象基类。我猜这可能会减轻更改接口的一些不便,但我认为开发人员应该感受到这种不便。

事实上,在理想的世界里,所有的接口都应该事先达成一致,永远不应该有改变的需要。我猜这是不现实的,但我认为没有必要让它变得容易。

简而言之,我认为如果您更改了一个接口,您应该考虑在所有实现的类中需要哪些变更,咬紧牙关,然后开始进行这些变更。

英文:

This won't be a popular answer, but it seems right to me that the operation you envisage is a difficult one.

Interfaces define a service contract between different sub-systems. Changing that contract should be more difficult that changing an implementation. Java 8 introduced "default" methods on interfaces, which converts the interface from being a strict contract, to a kind of abstract base class. I guess it might take some of the sting out of changing interfaces, but I think it's right that developers should feel that sting.

In fact, in an ideal world, all interfaces would be agreed in advance, and there should never be a need to change one. I guess that's impractical, but I see no need to make it easy.

The short answer, I think, is that if you change an interface, you should reflect on what changes are needed in all the implemented classes, grit your teeth, and get on and make them.

答案2

得分: 1

你应该始终遵循“开放扩展,关闭修改”的原则。
如果您添加新的方法,影响将在所有实现它的类中产生,您必须将该方法实现给所有类(无论是 Java 5 还是 8,直到它是默认方法),因此最好的做法是

创建一个新的接口,需要实现该接口的类应该扩展它。

英文:

you should always follow the principle of "Open for extension Close for modification"
if u add a new method the repercussions will be in all the Classes implementing it , U have to implement the method to all (be it java 5 or 8 until its a default method), so better

Create a new interface and the needful classes should extend it 

答案3

得分: 0

我们需要向接口中添加一个方法

好吧,那么就没有其他方法了。没有任何关于你正在添加什么以及为什么添加的上下文,我会相信你确实需要添加。确保情况确实如此。

Java 5: 添加这个方法。修复所有实现它的类。

你可以考虑在实现该接口的新抽象基类中添加一个默认方法,并使所有实现都扩展该基类。这样,如果将来再遇到类似情况,你会更容易处理。

Java 8: 添加一个默认方法

英文:

> we need to add a method to the interface

Well then there's no way around it. Without any context for what you're adding and why, I'll take your word for it that you do need to. Make sure that's actually the case.

Java 5: add the method. Fix all the classes implementing it.

You can consider adding a default to a new abstract base class which implements the interface, and make all implementations extend that. Then if you're in the same situation in the future you'll have an easier time.

Java 8: add a default method

huangapple
  • 本文由 发表于 2020年9月9日 18:00:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63809249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定