[java foundation] --- java development, service layer must write interface

It is estimated that many Java development students have encountered that the service layer must write an interface, and then write the implementation class of the interface, but this interface will not have a second implementation from the beginning of the project to the closure of the project, so why not write directly Service class? If you have n’t thought about it yet, think about it.

Netizens support the interface mode, the possible reasons are as follows:

1. Most of the projects are developed based on the spring framework. We know that the development style of spring is interface-oriented, so many people have moved in according to it. If you really only have this reason, think about it. It ’s not just learning tricks, not internal force.
2. The interface is polymorphic. Adding the interface will give you more flexibility. There is nothing wrong with that.
3. If multiple people develop, and each person is responsible for one layer, then the person in charge of service can define a set of interfaces for the students at the controller layer in the early stage of development, and slowly write after finishing. This kind of development mode is basically not available now. It is basically a development task divided by business module, and it is rarely divided according to this level.
4. Various design patterns will use interfaces, corresponding to multiple implementations. This is true. Java inherits and implements two methods, especially the interface implementation. Without this function, many design patterns can not be used. This is no problem.

my thoughts:

About the service layer interface.
Use when you should, but do n’t use it when you should n’t.
It can also be considered that the basic class should not be used, and complex business should be used.

The reasons are as follows:

1. A basic class, such as a user table user, corresponds to userService, it is difficult to have multiple implementations. After writing a userService for a basic project, it will not be changed for a long time, let alone multiple implementations, so this In the case of no need to write an interface, write a Service class: UserService. This is why many people have been puzzled since the beginning of coding, including me. At the beginning, I looked at the old code of the project, including some code on github, including some tools for automatically generating code plug-ins. This style of interface led me to For a period of time, I wondered whether I had a great defect in my knowledge point, so I could not see the benefits of this interface mode.
Of course, if your table corresponds to multiple data sources, you can consider using the interface mode, but generally it will not appear.
2. Complex services must use interfaces. For example, for an order center, an order placing interface, the basic input parameters are the product list and user information. Corresponding to different business situations, there will be different order implementations, and multiple business departments will call the same order interface. At this time, different interfaces will be implemented.

At this point, many people will use the standard word as a theory. Since complex services have to write interfaces, they should simply be written as interfaces. Isn't this good?
For this statement, I just want to refute with a sentence: Some things you obviously do not have until you do, but you have to insist on doing it, except for some people with obsessive-compulsive disorder, I really can't think of any reason for me to do it.

to sum up:

The service does not use an interface, which is not so complicated. I have summarized a few points for reference:
1. The service corresponding to a simple basic entity class does not need to use an interface, especially in the microservice mode.
2. In addition to the service corresponding to the simple basic class, it is best to use the interface mode for other services, because the business layer, no one dares to guarantee what expansion will be someday.

Say one more sentence:

In my opinion, in fact, many places can be omitted. For example, in the microservice mode, the service layer of a simple service can be omitted. But my premise is simple. For example, a department table only provides simple interfaces such as addition, deletion, modification, and inspection. When there are not too many complicated things, the service layer can be completely saved, the dao layer can write SQL, and the controller layer can be written.

In many places, I think we have to think boldly, not necessarily the previous ones must be right. If you find a place that is redundant and finds that it is indeed redundant after various examinations, you should delete him instead of Follow the current.

Otherwise, one day when someone younger than you ran over to ask you: "Senior, why do you need to add an interface to this service layer, and writing directly is not good?", You can only use: "I do n’t know, It has been written like this before, so I followed it up "to answer.

Published 203 original articles · praised 186 · 210,000 views

Guess you like

Origin blog.csdn.net/java_zhangshuai/article/details/104406039