java – When else to use interfaces

Question:

Faced the fact that often systems declare interfaces for each class from the service or repository layers. Accordingly, each interface has only one implementation. So, as it seems to me, this is overhead. I would like to know when all the same should be used interfaces, or rather, is it worth using them in such cases. Perhaps there are some good practices or smart articles about this.

Perhaps, different languages ​​have different practices, the situation with Java interests

Project: medium-sized enterprize monolith with two bases, interfaces for communicating with bases do not overlap in any way, tests are just a unit and calmly (as it seems to me) you can get by with implementation mocks

Answer:

I will assume that the interaction of layers is done through interfaces for ease of testing.

To make an object or group of objects easy to replace with other objects, an interface is the best, simplest, and most direct method: you let your testing code simply create another implementation of the same interfaces. This makes it much easier to replace layers above and below.

Scroll to Top