Why are there interfaces on the Service layer?
I was asked at will why every service layer has to write an interface, which is a lot of trouble ~ Although I want to talk about something, but I don't know where to start, I have to sort out my thoughts.
Scenario 1: There are many such cases in the open-source framework, that is, a function supports user-defined extensions. to put it bluntly, it provides an interface. We only need to implement this interface and add our own implementation logic so that the framework can be executed according to our logic. the question is, the framework author does not know what our implementation class is. If we do not define an interface, how can we call our implementation class in the framework?
Scenario 2: My colleagues and I work on two different functional modules of the project respectively, but some of the functions of my colleagues need to call the logic implemented by me. to make him have a "Placeholder" available, should I quickly write an interface and throw it to him?
Scenario 3: An adapter function, or a simple factory class. If no interface is defined, how can I perform operations in a unified manner in the face of many implementation classes?
Scenario 4: I want to make the project code conform to a certain "Specification", but it is impossible to look at other people to write code. It's easy to do. First, let's get an interface, and then you can look at it ~
Scenario 5: Java does not have multi-inheritance, but can implement multiple interfaces, so there is an interesting thing. An implementation class can implement multiple interfaces, then, some methods of exposing implementation classes can be selected for the interface to narrow down the class functions.
Of course there are many examples. These situations can be said to reflect the benefits of interfaces, so java has suggestions for interface-oriented programming. But do I have to have interfaces at the Service layer? This is not necessarily the case, because in the end, one more interface is only scalable and advantageous in some cases, but whether it will use the convenience of the interface, if we are not sure, we may not have to pay for the possibility. just write a few more lines of code, a little effort may avoid "the future" big "trouble", why not !?