简体   繁体   English

具有UnsupportedOperationException实现的默认方法

[英]Default method with UnsupportedOperationException implementation

I need to fix a bug in my project, but it turns out the root cause is an effect of many workarounds spread for all implementations of an interface due to a design problem. 我需要修复项目中的错误,但实际上,根本原因是由于设计问题而导致针对接口的所有实现散布了许多变通方法。 I want to refactor that interface, but I can't do it now, cause I don't have time to change all implementations. 我想重构该接口,但现在不能这样做,因为我没有时间更改所有实现。 So my ideia is to add a default method in this interface and implement it in only one implementation (partially fixing the design problem) and then making the big refactoring in next sprint. 因此,我的想法是在此接口中添加默认方法,并仅在一个实现中实现它(部分解决设计问题),然后在下一个sprint中进行较大的重构。 This refactoring is about replacing all methods in this interface for simpler and more meaningful ones. 这种重构是关于将此接口中的所有方法替换为更简单,更有意义的方法。 One of those new methods is the default method which the question is about. 这些新方法之一是问题所涉及的默认方法。

But actually there is no implementation needed for this method, its just a temporary solution which goes to the right direction. 但是实际上,此方法不需要实现,它只是一个朝着正确方向发展的临时解决方案。

Does it make sense to implementing that default method (in the interface, of course) throwing an UnsupportedOperationException? 实现该默认方法(当然是在接口中)并抛出UnsupportedOperationException是否有意义?

The whole idea behind default methods is to provide for adding methods to an interface without automatically breaking existing implementations. 默认方法背后的全部思想是提供向接口添加方法的功能,而不会自动破坏现有的实现。 It seems like that is what you propose to do, so that makes sense as far as it goes. 您似乎打算这样做,所以就目前而言,这是有道理的。

But in comments you remarked: 但您在评论中指出:

I'm gonna override it in the implementation class that I need to fix the bug and then call this new method instead of the previous one which doesn't make sense. 我将在实现类中重写它,我需要修复该错误,然后调用此新方法,而不是前一个没有意义的方法。

If you are going to be invoking the new method on any instance of your interface, then you need to be confident that it will do something appropriate. 如果要在接口的任何实例上调用新方法,则需要确信它会做一些适当的事情。 If there's any chance that such an invocation is served by your proposed default implementation that always throws UnsupportedOperationException (and supposing that would be undesirable), then that probably does not justify such confidence. 如果您的提议的默认实现可能会始终引发UnsupportedOperationException (并且假设这是不希望的),则有可能导致这种调用,那么这可能并不能证明这种信心。

If you somehow do have justified confidence that the new method will be invoked only on instances of one specific implementation class then either there is something very strange about the way you are using interfaces, or you don't actually need to change the interface at all. 如果您确实有理由相信仅在一个特定实现类的实例上调用新方法,那么您使用接口的方式会很奇怪,或者实际上根本不需要更改接口。 That is, if you know what implementation you are working with, you can add the new method to that class alone, without changing the interface. 也就是说,如果您知道正在使用哪种实现,则可以将新方法单独添加到该类中,而无需更改接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM