This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.


  • 💬 if the article is helpful to you, welcome to pay attention to, like, collect (one key three links).

🐹 1. Preface

  • Hello everyone, MY name is Kocheng. It’s time to learn again. Recently, I received contributions from my friends. To tell you the truth, I have interviewed many companies before, and I have never encountered this problem, so I feel quite novel, so I write this article to record learning, because my level is limited, when looking at the problem may not be comprehensive, if you have different views, please leave a comment below for discussion.

  • If the article is helpful to you, you can help a key three even oh! If you encounter some strange or novel topics in the interview, welcome to submit a private letter, thank you for reading!




🐣 two, the purpose of the interviewer so ask


When an interviewer asks a question, we need to try to figure out what the interviewer is asking, instead of responding right away, so that we can give a more comprehensive answer. Here’s what I guess the interviewer is trying to say:

1. See if you know anything about interface programming (not surprisingly, many people have only heard of it, and there is a vague concept that can’t be explained clearly)

2. See if you understand the purpose of interface oriented programming (if a technology/idea comes along that doesn’t solve some existing problem, what is the value of its existence?)

3. See if you’re really applying interface oriented programming to development

🐰 3. What is interface

Before introducing interface oriented programming, let’s take a look at what an interface is so that we can “know what it is and why.”

Interface, named Interface in English, can be understood as a standard (specification). In a broad sense, it refers to a set of standards, which stipulates that the class or Interface implementing the Interface must also have this set of rules. Of course, there is a special case, namely empty Interface (Interface without any method), which will be introduced later.

In an object-oriented system, the system between various functions between classes, small to big to the interaction between the module, the system is actually implemented by the collaboration of different objects, in the design phase, will not be too concerned about the internal implementation details, but focuses on the collaborative relationship between the design object, try to achieve the goal of high cohesion and low coupling.

Interface as a form of entity of abstracting, used for pulling away external communication and internal implementation to achieve internal changes without affecting the external interaction with other, can understand as a way for design programming according to this idea can be referred to as the programming to an interface (now speak more abstract, visualization through specific examples below).

Here’s an example: As the USB port on the computer, if you feel wired mouse with not enough, can buy a wireless mouse inserted into the original wired mouse port seamless switching can be realized, and don’t need to modify the computer anywhere, no matter whether your wireless mouse and wired mouse is the same type, because the socket is a standard, As long as the mouse you buy meets this standard, it can use the corresponding mouse through this socket.

PS: USB, short for Universal Serial Bus (not you’re a fool….) Is an external bus standard that regulates the connection and communication between computers and external devices. It is the interface technology applied in PC field.

🐷 4. Comparison of procedural programming, object-oriented programming and interface programming


In the process of coding, you will hear the introduction of these three. Sometimes, although you have a general understanding of their concepts, you always feel that you do not understand when you express them. The following is a more visual and intuitive re-understanding of them through specific examples.


🍅 4.1 Procedural Programming (POP)


Procedure Oriented Programming (POP) : When solving a problem, first analyze the steps needed to solve the problem, and then use functions to realize these steps step by step. Then when dealing with the problem, call this function one by one in a certain order, and the problem is solved after the method is executed.

Illustrate for example: stomachache, how to solve? (This is a smelly example, but in order to make everyone understand, so the example is more close to life, if you feel too smelly, leave a comment below, next time for a better example)

If you use procedural oriented programming to solve this problem, the flow would look something like this:

1. Perform the method of finding paper towels (don’t use your hands to find paper towels?)

2. Perform the toilet sitting method (you can’t stand without a toilet?)

3, the implementation of human food residue excrement method (ah, comfortable!)

4, the implementation of cleaning method (not clean, don’t pick up the pants to go?)

5, the implementation of the toilet flushing method (do not flush! It’s you who stink!




Procedural Programming summary:

As can be seen from the above examples, the idea of procedural programming is to solve a problem by breaking it down into steps, encapsulating each step into a corresponding function, and then executing it in a certain order.


🍑 4.2 Object-oriented Programming (OOP)


Object Oriented Programming (OOP) : Such as C++, JAVA and other languages), focuses on objects. When solving a problem, the objects contained in the problem are first abstracted into object concepts, which contain specific attributes and behaviors. When it is really implemented, each object is asked to execute some methods of its own, so as to solve the problem.

Illustrate for example: stomachache, how to solve?

The process of solving this problem using object-oriented programming is as follows:

1, according to the entity involved in the problem, abstract the object of “people”, “toilet” object, “paper towel” object

2, add some properties and methods for the “people” object, such as xx 28cm, black long straight. Methods: Find ways to use paper towels, sit on the toilet, remove excrement, and clean xx tools

3. Add some properties and methods for the “toilet” object, such as white, 15m long, oval. Method: Flush the toilet

4. Add properties and methods to the Paper Towel object, such as white/black/gold

5. Execution:

One object. Finding paper towels

One object. Sit on the toilet method

One object. Method of excreta removal

Toilet. Flushing method (yes, the toilet is self-sensing, rich people can play like this, rich people’s life is comfortable)

One object. Clean xx tool method (clean where I don’t have to say you also understand! DDDD)




Object-oriented programming summary:

It can be found through examples that the object oriented programming method to solve the same problem is to abstract the entity in the problem into concrete objects, and then encapsulate the attributes and methods into objects, and finally solve the problem by implementing corresponding methods in different objects.


🍒 4.3 Interface Oriented Coding (IOP)


Interface Oriented Programming (OIP) is a kind of Programming idea, Interface is abstracted as a form of entity, used to separate the internal implementation for external communication, and finally realize internal changes without affecting external interaction with other implementations. The way programming is designed with this in mind is called interface oriented programming.

It is not more advanced than object-oriented programming an independent programming ideas, can be said to belong to a part of the object-oriented thought system or it is one of the essence of object-oriented programming system.




Note: The red boxes in the figure above are some of the features that distinguish object-oriented programming.

The above examples are both process-oriented and object-oriented. Combining our experience in daily life, we can know that both men and women may have stomachache and need to go to the toilet, but different people have different ways to solve this problem. It is not possible to modify the original code every time someone has a different approach, which not only violates the open and closed principle of object-oriented programming, but also creates potential risks.

Therefore, we can be the object oriented programming upper the toilet five steps to take to the [people] stomach processing interface and 】 【 】 toilet interface, which people upset stomach can achieve this man’s stomach processing interface), each type toilet implement “toilet interface” (because not every toilet clean up details, have a plenty of automatic, have a plenty of manual).

Specific how to go to the bathroom, flush the toilet defined by yourself, you can stand up, sit up, can be upside down, so that to reach the purpose of the external changes without affecting the internal interaction, I use this interface type receive implementers of implements this interface, the implementer changed the method of logic, does not affect I receive it, make the method call.

🐴 5. Features of procedural programming


Advantages:

Problems are broken down into steps, like an assembly line, and implemented step by step to simplify the process of complex problems.

The performance is higher than that of object-oriented programming. In object-oriented programming, the class call process needs to be loaded and instantiated, which consumes more resources. For high performance requirements such as single-chip, embedded development, Linux/Unix, generally adopt process-oriented development.

Disadvantages:

No object-oriented programming is easy to maintain, easy to reuse, and easy to extend

Usage Scenarios:

Suitable for high performance requirements of the system development

🐓 6. Features of object-oriented programming


Advantages:

Easy to understand: the use of object-oriented design and development, more in line with the way of thinking, high readability.

Easy maintenance: Object-oriented has the characteristics of encapsulation, inheritance and polymorphism. Even if the requirements change, more local modules need to be maintained, which is more convenient and cheaper to maintain.

Easy to expand: Object-oriented has the characteristics of encapsulation, inheritance, polymorphism, in the design stage of the system can be designed with high cohesion, low coupling system structure, making the system more flexible, easier to expand.

Easy reuse: similarly, because of the three features of object oriented, common code can be easier to reuse, improve development efficiency.

Disadvantages:

Performance is lower than process oriented and programming complexity is higher

Usage Scenarios:

User needs are constantly changing, Internet applications, games, enterprise applications

🐋 The role of interface – oriented programming


While explaining some of the features of interface oriented programming, let’s take a look at two of the five design principles of object-oriented programming:

🍑 7.1. Conform to the principle of open and close, dependence inversion and enhance extensibility


Open-close Principle (OCP) : Open for expansion and closed for modification.

DIP(Dependency Inversion Principle) abstraction should not depend on details. Details should depend on abstractions

In software development, system extensibility exists all the time because the business is constantly changing. Taking database as an example, before the introduction of interface, due to the small business volume at the beginning of the project, we would directly implement a series of operations on a database through the code during the design, which is abstracted as shown in the following figure:




However, with the increase and complexity of business, a single traditional relational database cannot meet our business, so we need to introduce non-relational database as the middle layer and more advanced database as the data storage, which is abstracted as follows:




Obviously, because our business was simple and we only considered one kind of database at the beginning, the code to operate the database was strongly dependent on the specific implementation class. Now, due to business changes, we need to introduce other different databases. In order to achieve this goal, we have to modify the original business code. Such modification obviously violates the open and close principle of object-oriented programming, the dependency inversion principle and brings potential risks. Therefore, such design is not reasonable, so we introduce the interface, which is abstracted as follows:




As shown in the above, there is an interface, which is a standard of our JAVA program without directly dependent on the corresponding implementation class, each database vendors implement JDBC standards, to provide users a driver, if we change because of business need to introduce a third party database, you just need to modify the corresponding driver, do not need to change the original logic, Not only does the business code no longer depend on the implementation class, but the implementation class depends on the interface, namely the dependency inversion principle “abstraction should not depend on details, details should depend on abstraction” and the open and closed principle [open to expansion, closed to modification], but also avoids the potential risks brought by changes.

The specification is defined in the interface, and different implementers implement the specification according to their own requirements. If there are new requirements, new implementers can be created to implement the interface specification, so as to achieve loose coupling of the code.

The most common example is JDBC(JAVA Database Connection), which is a JAVA API for executing SQL statements. It can provide unified access to multiple relational databases. It is the standard specification for JAVA to access databases.

No rules is lacking, if there is no JDBC access to the database operation, there is no specification, all database vendors will have its own set of database access rules, once the company database from the switch, need to write the whole involves database code, it is the end of the world for developers.

So the sun’s unified database specification, different database vendors need to provide the specification of implementation of a drive, used to communicate while developers to connect to the database, with JDBC, developers don’t need to pay attention to how to write for different database access code, change the database, you just need to switch to a different drive.

PS: JDBC is the interface, the driver is the implementation of the interface, no driver will not be able to complete the database connection, thus can not operate the database! Each database vendor needs to provide its own driver to connect to its own database, which means that drivers are generally provided by the database generator vendor.

🐇 the relationship between abstract classes and interfaces


Seeing this, I believe many friends will question whether to use abstract class to replace the interface, after all, the concept of the two is very easy to blur. In my opinion, the most important thing in JAVA, besides the restriction that classes can only be inherited singly, but interfaces can be implemented more than once (interfaces can be inherited more than once), is that both are designed for one purpose.

Abstract classes are designed to extract some common features or default behaviors of a class for code reuse. Interfaces, on the other hand, are designed to specify a standard, which emphasizes the specification and, in object-oriented languages, the use of polymorphism. Therefore, if there is a scenario where the design is abstract class or interface tangled, it is recommended to consider the motivation of the design, which should get a better result.

🐈 9. Mark interfaces


Features: A Market Interface, also called a Tag Interface, represents an Interface without any methods or attributes. It simply indicates that the class being tagged belongs to a specific type, allowing other code to test to do something.

The tag/marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata.

Markup interfaces are not unique to the JAVA language, but are a computer science design concept used to describe objects to object-oriented languages.

Because the programming language itself does not support maintaining metadata for classes, the tag interface can be used to describe metadata for classes, compensating for this lack of functionality. For classes that implement the tag interface, we can retrieve metadata at run time through reflection.

In the JDK, the most commonly encountered markup interfaces are as follows: If a class implements the Serializable interface, it can be Serializable. If a class implements the Serializable interface, it can be Serializable. It can be retrieved at run time by reflection and then performed a series of operations.

In JAVA, markup interfaces are used in two main ways:

Create a public parent interface:

Just like the EventListener interface, which is a Java API extended by dozens of other interfaces, you can use a tag interface to create a parent interface for a set of interfaces. For example, when an interface inherits the EventListener interface, the Java Virtual machine (JVM) knows that the interface will be used to broker an event.

Add a data type to a class

This was the original purpose of the tag interface. The class that implements the tag interface does not need to define any interface methods (because the tag interface has no methods at all), but the class is polymorphized into an interface type that can be used to receive instances of the concrete class that implements it.

🐧 10. Some problems about the practical application of interface – oriented programming


10.1. Why is it necessary to use the interface + implementation class approach when there is only one implementation class in many projects, from controller to Service to DAO?

I bet there’s a lot of debate on this issue, and everyone thinks they’re right. It’s hard to convince someone who uses interface + implementation classes to convince someone who uses implementation classes directly, and vice versa.

First, support: interface + implementation class mode, the reasons are as follows

Claim 1: If you are using the Spring framework for project development, this approach is recommended to maximize the use of Spring IOC, make it easier to use and reduce memory usage.

Idea 2: Maintenance and extension benefits. You now have only one implementation class, but with complex business changes, multiple implementation classes may appear later. This way, if there is a new business change, you can simply add a new implementation class without changing the original code.

Idea 3: It is good for teamwork. Different people may be responsible for the development of different modules such as Controller, Service, and Dao. Once the interfaces are defined, the person responsible for each module can be responsible for the corresponding development.

Claim 4: Control exposure to external content to ensure security issues. Interfaces only expose the public parts of the definition, but using objects directly can see the private parts, which may have security issues.

Claim 5: This is a programming specification that encapsulates the implementation details and exposes the external interface. This is also the idea of “high cohesion, low coupling”.

Idea 6: Follow the open closed principle, reduce the risk of change, easy to understand, because many source code is also used in this way.

Claim 7: Using polymorphism, you can have multiple implementation classes on the same interface, which can be coupled with reflection and configuration files to decouple the code. When you need to enhance functionality, you can simply add new classes and modify the configuration files to achieve this goal.

Two, do not support: interface + implementation class mode, support direct use of implementation class mode, the reasons are as follows

View 1: By ninety-nine percent, most of the actual project experience, the realization of the interface and implementation classes are 1:1, in this case, the more defined an interface is not necessary, but also need to maintain an additional code, so there is no need for standard and specification, if it is a small team, company’s technical level is average, directly using the implementation class, it is not necessary to define interfaces.

🐉 11. Write at the end


Ok, about the introduction of interface programming is introduced here today, we have different views welcome to leave a message below, welcome everyone to contribute to me in the interview encountered strange problems oh, learn to run, hurriedly take the article to find an interviewer on the line. If this article helps you, don’t forget the triple link!

🐬 xii. References


1. JAVA tutorial

2. JAVA markup interface