A "interface blowing" monologue

@[TOC] (article list)

Preface

This article mainly uses the idea of ​​software engineering as a starting point and combines some examples to deeply investigate the role and definition of interfaces


1. What is an interface

Before explaining member variables, this article needs to familiarize readers with what an interface is and what the interface does.

1.1. What is an interface?

An interface can be understood as a "class" that is more abstract than an abstract class. Its attribute variable types can only be constants, and methods can only be abstract types. And the properties and methods need to be public for direct external call.
This also shows that every method an interface has on it is just a method, and there can be no implementation body. That is to say, all the expressions that cannot be expressed in all methods need to be modified by others' preferences (whether there is a high-level "gutter", hahaha), and work hard.
But from another perspective, the interface is not it also be understood as not being a complete outline of the white paper , the white paper may have some attributes can not be changed (eg: materials, basic tone, etc.), the analogy of empathy The "abstract category" is like a picture that has already taken shape (similar to the busy protagonist Du Fu in the elementary school textbook), which can be sketched at will, but compared to "white paper" (interface), "picture "(Abstract class) already has some exclusive shapes (methods that have been implemented by itself, such as facial features, clothing, etc.). Of course, you can also modify the color of some parts of Du Fu's picture (this is equivalent to an abstract method implementation). Or do "plastic surgery" on Du Fu's "five senses" (similar to method rewriting). But in general, abstract classes and interfaces have their own characteristics, but the interface is more like a white paper from the perspective of paper than abstract classes.

2. What does the interface do?

2.1, design mode perspective

From the perspective of software engineering , design patterns have six principles: the single responsibility principle, the Richter substitution principle, the dependency inversion principle , the interface isolation principle, the Dimit's principle, and the opening and closing principle. The principle of dependency inversion is inseparable from the interface.

Dependency inversion principle : Class A depends on class B, but if the direct dependency relationship is adopted, the coupling degree of the design will be improved. Therefore, in order to reduce the direct coupling degree between class A and class B, an interface C is abstracted at this time, this interface C Contains public methods in class A and class B and constant public A and B calls. At this time, A and B directly depend on interface C.
It can be seen from the design pattern direction of software engineering that interfaces are used to
help decoupling .

2.2, three characteristic angles of Java

From the three major characteristics of Java : encapsulation, inheritance and polymorphism. The interface strictly embodies these three characteristics.

1. Package

The first is encapsulation . Someone may say directly, how can the idea of ​​encapsulation be reflected in the attributes of the interface if the attributes of the interface are public? Isn’t encapsulation hiding the attributes and calling them only by trusted members (of course, most of them are only trusted by themselves), so as to ensure Is the data safe?
In my opinion, the second half of the previous paragraph is the purpose of encapsulation, while the first half is just a means. Hidden attributes only provide a way to ensure security, and if there is another way to ensure security, it cannot be said to be wrong. The interface provides another way to ensure data security -----turn all attributes into public constants . At this time, it not only ensures that the attribute can be called, but also strictly ensures the security of the attribute. So I said it provides a special " package ".

2. Inheritance

The second is inheritance : the essence of inheritance is to improve code reusability. Compared with abstract classes, interfaces provide multiple inheritance implementations, and the diversity is greatly improved compared with abstract classes. But this should ensure that the interface strictly reflects this feature. And in terms of inheritance, the more advanced things (in other words, the less restrictive and the greater the scope), the higher the audience, and the larger the inherited record. Similar "people" are less restrictive than "men", so the scope is higher and the reusability is higher.

3. Polymorphism

Finally, there is polymorphism : =-=Well, although I am an "interface blowing", this interface does not reflect it. After all, the positioning of the interface is to provide basic and inorganic 0-addition configurations for a large group of people without room. We do not provide "multiple" "State", only to provide a platform for all audiences who want to achieve "polymorphism" (I really want to hear that).

3. Why must interface attributes be constants?

The member variable of the interface is public+static+final
public: this variable can be called directly from outside.
static: The variable is shared by the class, not just for an instance.
final: The variable cannot be modified once assigned.
At this time, its characteristics are ready to emerge: the only unmodifiable constant that can be called and owned by subclasses.

3.1、OOP

3.2, packaging

to sum up

Continuously updated

Guess you like

Origin blog.csdn.net/JasonLee97/article/details/109217547