Why are there interfaces on the Service layer?

Source: Internet
Author: User

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 !?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.