Do I need an interface that has only one implementation?

I have an interface that has only one implementation. The next couple is a simple java object. I want to remove this interface and use the object directly. But I want to understand when it is needed and why it was designed in this way. This was done, of course, so as not to weaken the coverage of the unit test. So why is there a single implementation interface in my project? Thanks.

+4
source share
5 answers

The interface is useful when you can expand / modify your project. For example, if you run a project by saving the data in a text file, and then decide to go to it from the database. If both of them are implementations of the same interface, then replacing the first for the second is very simple. Just a case of replacing a specific implementation in a class that uses it. For example, simply changing

IStorage storage = new FileStorage(); 

to

 IStorage storage = new DBStorage(); 

Although it might seem pointless to have an interface for one implementation, it can save you a lot of refactoring efforts later.

+10
source

If you define an interface for an implementation and use the interface in your program, you can always write a new implementation and replace it with an older one without changing the code of the classes that use it.

It is best to use an interface rather than an implementation in this way; changing requirements is less harmful.

+1
source

It depends. But this is not such a bad idea.

In C and in many versions of Pascal, separating an interface from an implementation is a common practice. This helps the compiler avoid unnecessarily recompiling modules when the implementation on which they depend changes.

In Java, the compiler is usually not a concern. Java provides public, secure, private, and private (implicit) access classes that limit how much other classes may depend on the details of a particular class. Javadoc provides (by default) documentation that omits unnecessary and unnecessary details. We also have the widespread YAGNI principle: if "you don't need it," don't develop it.

Nevertheless, using an explicit interface in Java, rather than using an implementation directly, provides an additional opportunity to clearly understand what structure and behavior users of a certain class should influence. The ability to implement multiple interfaces means that users of a particular class can even determine the dependence on one or more specific aspects of the implementation, such as its Serializable nature.

+1
source

Sometimes you think that using the interface will be useful when it turns out that you have only one implementation. This is not always bad, but sometimes it is just a waste of effort.

From the principles of YAGNI

Always do things when you really need them, never when you just foresee that you need them.

+1
source

If you are doing unit tests for your code (I hope you do), there is a big chance that you will need to mock using your class with some kind of stub implementation. So you need an interface, at least for unit tests.

PS I know that modern bullying structures like Mockito can do class-based bullying, so technically you can achieve this without interfaces. But (for me) this seems like a hack, as this is exactly where the interface should be used.

0
source

All Articles