Answer the question
In order to leave comments, you need to log in
Why do we need interfaces for DAO and Service layers in Java?
Hello! Recently I have been learning Java and Spring from video tutorials. I saw code where interfaces are created in the service and DAO layers first, and then their implementation, and work is underway with this implementation. The very meaning of interfaces from OOP is clear to me, but why exactly are they needed in these structures. I can not understand. Thanks in advance!
Answer the question
In order to leave comments, you need to log in
Interfaces are needed to be able to replace the implementation without affecting the main code. When using interfaces, the main code knows nothing about implementation details (loose coupling). Accordingly, the implementation can be moved to a separate module (to isolate the complexity). If the main code refers directly to the class containing the implementation (strong coupling), then the complexity of the program increases and the maintenance of the program becomes more complicated. Therefore, interfaces are especially needed at the boundaries of subsystems.
If the implementation is separated from the main code, then it becomes easy to replace it in tests, for example, slip the implementation in memory instead of the database. See fake object (not to be confused with mock). This is another important plus of interfaces.
Perhaps, in simple programs, interfaces are not very necessary. But, since you are talking about a training example, then using them (accustoming immediately to a good, correct style) is correct.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question