[Zen Reading Notes in design mode] 004 _ six principles of design mode (4): interface isolation principle

Source: Internet
Author: User

Preface

I have to say that I have seen another principle that is more difficult, because it is another principle that changes one class to two and two to four. In a single responsibility principle, in order to separate different responsibilities, we abstract a class into N interfaces. The interface isolation principle in this article is also an abstract multi-interface principle, I can only sigh with God: I am not writing a class! How many interfaces are you going to split out?

So what is the relationship between the interfaces abstracted by the interface isolation principle and the interfaces abstracted by the documentary responsibility? What is interface isolation?

Body

I. Concept of interface isolation

To understand this concept, first, let's understand what isInterface. Those who have learned Java must naturally say that interfaces define interfaces. Yes, this is one of the interfaces calledClass Interface. It is an abstraction of classes.

Are there other interfaces besides class interfaces? Yes. The so-called interface is abstraction. The interface is the abstraction of the class. Who is the abstraction of the class? Yes, it is an abstraction of the instance. That is to say, classes are also interfaces, which are calledInstance Interface. For example, I have a Chinese class, which is an abstraction of the Chinese people. It has a membership function that eats leather shoes and drinks toxic milk, and a definition body. The Chinese class is an instance interface. If you are not talking to me about the abstract of the Chinese class, I am speechless. Haha! Now there is a class that represents human beings. It is an abstraction of all human beings on the earth and an abstraction of the Chinese class. It has a member function of eating. This is a class interface.

Then, let's take a look at what isolation is. The core of isolation is as simple as possible-there are as few member methods as possible. What does this mean? The single responsibility principle requires us to Abstract Our interfaces from the responsibility perspective, that is, the business perspective, and segment our interfaces. This interface isolation principle segments interfaces from the code perspective. Although we have a single responsibility for an interface subdivided from a single responsibility, we sometimes find that even if we have applied a single responsibility principle, there are still many methods in the interface, at this time, our interface isolation principle is required.

2. Connection Between the interface isolation principle and the single Responsibility Principle

The single responsibility principle is to abstract and segment interfaces from the business perspective, while the interface isolation principle is to segment interfaces from the code perspective. Because the single responsibility principle may obtain a large number of interfaces, we need to implement the interface isolation principle.

Iii. Four-layer meanings of standardized constraints on interface isolation principles

The interface isolation principle is to standardize the interface, which has the following four meanings.

1. The interface should be as small as possible

The interface should be as small as possible. Obviously, if there are 100 methods in the interface, then every time I inherit this interface, I will implement these 100 methods. That's not exhausting, besides, I certainly cannot use all of the 100 methods. We must avoid bloated interfaces. However, we should not be too small in details. If you say that I put 100 methods into 100 interfaces, the boss will not beat you, people inheriting your interface will also hold the mouse at your head.

2. High Cohesion of interfaces

This is also the requirement of the above point, that is, to achieve high cohesion when refining interfaces, what is high cohesion? Many people may have heard of this concept, but have not carefully thought about the meaning of this high cohesion. I personally don't have a good definition, but I won't talk about it if I can.

3. customized services

This concept looks advanced, and I don't know if it is the author's prefix. The so-called custom service is to provide callers with only the methods they need. How can we provide only? It's also very easy!

4. Limited Interface Design

This is nonsense and a waste of ink. The interface design must be limited, but the author may want to remind us! Interface design requires experience, such as cooking, how much water is put in the end, only the people who have cooked the food know, put 2cm away from the meter can be like.

Summary

The interface isolation principle is quite simple, so pay attention to the distinction between it and the single responsibility principle. That is, the former is the code layer, and the latter is the business layer.

I don't know if there is any difference. Please also pick it up.

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.