简体   繁体   中英

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. 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. 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 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. Javadoc provides (by default) documentation that omits the unwanted and unneeded details. We also have the widely promoted principle of YAGNI: if "you ain't gonna need it", don't engineer it.

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. 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.

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

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. But (for me) it feels like hack, as this is exactly the place where interface should be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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