简体   繁体   English

我是否需要只有一个实现的接口?

[英]Do I need an interface that has only one implementation?

I have an interface, that has only one implementation. 我有一个接口,只有一个实现。 Following pair represents a simple java object. 以下对表示一个简单的java对象。 I want to remove that interface and use an object directly. 我想删除该接口并直接使用对象。 But i want to understand when it's needed and why it was designed in that way. 但是我想知道什么时候需要它以及它为什么以这种方式设计。 It was done definitely not to ease unit test coverage. 这绝对不是为了减轻单元测试覆盖率。 So, why there is an interface with only one implementation in my project? 那么,为什么在我的项目中只有一个实现的接口呢? Thanks. 谢谢。

An interface is useful when you might extend/change your project. 当您可以扩展/更改项目时,界面很有用。 For instance, if you start a project by storing your data in a text file, and then decide to change to doing so from a database. 例如,如果通过将数据存储在文本文件中来启动项目,然后决定从数据库更改为执行此操作。 If both of these are implementations of the same interface, then swapping the first out for the second is very simple. 如果这两个都是相同接口的实现,那么将第一个接口换成第二个接口非常简单。 Just a case of swapping the concrete implementation in the class that uses it. 只是在使用它的类中交换具体实现的情况。 For instance, simply changing 例如,简单地改变

IStorage storage = new FileStorage();

to

IStorage storage = new DBStorage();

Whilst it may seem pointless to have an interface for a single implementation, it may save you a lot of refactoring effort later. 虽然为单个实现提供接口似乎毫无意义,但它可能会为您节省大量的重构工作。

If you define interface for an implementation and use the interface in your program you are always free to write a new implementation and replace it with older one without changing the code of classes that use it. 如果为实现定义接口并在程序中使用接口,则可以随意编写新实现并将其替换为旧实现,而无需更改使用它的类的代码。

it is a best practice to use interface not implementation this way changing requirements are less harmful. 最好的做法是使用接口而不是以这种方式实现更改要求的危害较小。

It depends. 这取决于。 But it's not such a bad idea. 但这不是一个坏主意。

In C and many versions of Pascal, separating interface from implementation is the familiar practice. 在C和许多版本的Pascal中,将接口与实现分离是熟悉的做法。 It helps the compiler avoid unnecessary recompilation of modules when an implementation on which they depend changes. 它有助于编译器避免在它们所依赖的实现发生更改时不必要地重新编译模块。

In Java the compiler is usually not the concern. 在Java中,编译器通常不是问题。 Java provides the public, protected, private, and package-private (implicit) access classes that limit how much other classes can depend on the details of a particular class. Java提供public,protected,private和package-private(隐式)访问类,这些类限制了其他类可以依赖于特定类的详细信息的程度。 Javadoc provides (by default) documentation that omits the unwanted and unneeded details. Javadoc(默认情况下)提供了省略不需要和不需要的详细信息的文档。 We also have the widely promoted principle of YAGNI: if "you ain't gonna need it", don't engineer it. 我们也有广泛推广的YAGNI原则:如果“你不需要它”,不要设计它。

Nevertheless, using an explicit interface in Java rather than using the implementation directly provides an additional opportunity to be quite clear about what structure and behavior the users of a particular class should depend on. 尽管如此,在Java中使用显式接口而不是直接使用实现提供了一个额外的机会,可以非常清楚特定类的用户应该依赖什么结构和行为。 The ability to implement multiple interfaces means that users of a particular class can even specify dependence on one or more specific aspects of an implementation, such as its Serializable nature. 实现多个接口的能力意味着特定类的用户甚至可以指定对实现的一个或多个特定方面的依赖性,例如其Serializable性质。

Sometimes you think using an interface will be useful when it turns out you only have one implementations. 有时候你认为使用一个接口会很有用,结果你只有一个实现。 This is not always a bad thing, but sometimes it is just a waste of effort. 这并不总是坏事,但有时这只是浪费精力。

From the principles of YAGNI YAGNI的原则

Always implement things when you actually need them, never when you just foresee that you need them. 当你真正需要它们时,总是要实现它们,永远不要只是预见到你需要它们。

If you are doing unit-tests for your code (I hope you do), there is a big chance you'll need to mock usage of your class with some stub implementation. 如果您正在为代码进行单元测试(我希望您这样做),那么您很可能需要使用某些存根实现来模拟您的类的使用。 So you need interface at least for unit-tests. 所以你至少需要接口才能进行单元测试。

PS I know, that modern mocking frameworks like Mockito can do mocks based on classes, so technically you can achieve this without interfaces. PS我知道,像Mockito这样的现代模拟框架可以根据类进行模拟,所以从技术上讲,你可以在没有接口的情况下实现这一点。 But (for me) it feels like hack, as this is exactly the place where interface should be used. 但是(对我来说)感觉就像黑客一样,因为这正是应该使用界面的地方。

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

相关问题 如何测试没有实现的接口 - How do I test an interface that has no implementation 我是否需要测试接口的每个实现来测试接口? - Do I need to test each implementation of an interface to test the interface? 当只有一个实现类时,为什么要使用接口? - Why should I use an interface when there is only one implementation class? 需要提取一个接口,但是实现中多了一个方法 - Need to extract an interface, but one method is extra in implementation 仅存在于接口的一个具体实现中的 function - A function that are existing only in one concrete implementation of an interface 我是否需要第三方接口的实现才能完成我的代码? - Do I need an implementation of a third party interface to be able to complete my code? 自动装配只有一个实现的接口-不满意的Bean依赖关系 - Autowiring an interface having only one implementation - Unsatisfied bean dependency 我是否需要同步访问仅由一个线程修改的List? - Do I need to synchronize access to a List that is only modified by one thread? Mockito-接口方法在我需要调用实现类时被调用 - Mockito - Interface method is called when I need it to call the implementation class 我是否需要在实施时再次使用@Nonnull? - Do I need to @Nonnull again at the implementation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM