Interface

xiaoxiao2021-03-06  103

Interface

First, why use the interface.

1. The interface can be relieved, and the isolation changes.

According to DIP (dependent reversal) principle, the specific class should depend on abstraction is the interface. The holder of the object should not hold the object directly, because this kind of change will affect the holder, and any modifications to the holder may result in the modification of the holder, at least Leading the holder's recompilation and testing. As the system grows, this phenomenon will become more serious, and finally, the system is difficult to modify or any small modifications will be a big project. This is the disaster brought by high object coupling.

However, the interface can relieve this coupling, and the holder object is just the interface of the object rather than a specific object, and the holder has implemented this interface. With this holder and the holder, there is no direct dependency, they depend on the interface (abstraction). We know that the specific classes are often volatile, and the interface is often more stable - if your interface is defined (how to define the appropriate interface, it will be discussed later). As a result, the interface is isolated, and the interface is isolated and will not spread to the holder object. See my "interface and mask"

2, the interface simplifies the customer programming and reduces the user's programming difficulty.

First, the interface relative to the specific class, there is a simple feature, which only defines the necessary behaviors and methods, without any detail information, so it is more simple than the specific class. Simply put the information that reduces the user needs to understand.

Second, the interface is characterized by the specific classes, clearness and uniqueness. There may be a large number of methods similar to or even the same function (for example using a two-way adaptation mode), which will make the user from. These issues do not appear in terms of use definitions.

Therefore, the surface of the interface limits communication between the objects, and simplifies this communication.

3, the interface specifies the behavior of the user and the user.

For users, it will only use interface definition methods, and these methods must be implemented for the user's implementation class for the user. This has achieved a contractual relationship.

This is especially important for multiplayer collaboration. Different users use the same specific class, because of the different understandings, it may be different, and some may be a willingness to violate this specific class. To achieve the same function, you may also use your own habit. This causes the code style and unparalleled, which is very disadvantageous for modifications and maintenance.

After using the interface, the specific class will hide the unified specification of the interface, the style of the code will be more aggravated, and the user and the user do not need to be worryable with each other, easier to modify and maintain.

The above two points, always talk about the interface that provides access to each other between the interfaces. Through this standard, the purpose of communication is achieved both the purpose of communication and the behavior of each other.

4, the interface can provide polymorphism.

Due to the isolation and normality of the above, there is no direct relationship between the user and the user, and can be independent and unaffected.

For the user, it can be used correctly as long as the user is in line with the interface it uses. And don't care what is used, and how to implement the interface. For a predefined interface, the user can be used correctly. It doesn't care how to use it. That is, for the same user, we can achieve different behaviors for different users, so that polymorphism is achieved.

5, the interface is the foundation of the design pattern.

The design pattern is an object-oriented essence, and the use of the interface is the foundation of the design mode. Throughout the 23 classic models of the "Four Gang", most of them use the interface. Even some people think that "design mode will be interpreted for interface programming." The process of learning design model can be used is the process of learning interface, and vice versa. Of course, the use of the interface may increase a certain complexity, but I think it is negligible than the above advantages. Everyone is now advocated for interface programming (interface programming is much wide than the scope of the scope here.) More indicated that the importance of the interface has been more and more people know. Since the interface is so useful, how can I use it?

Second, how to use the interface.

1, the definition of the interface.

To use an interface, first define the interface. Good interface definition is the basis for rational use of interfaces. What kind of interface is a good definition interface? It should have high internal polymerization, low coupling and other features. There is also an easy to understand.

The naming of the interface involves the starting point problem for an interface definition. Is the interface from the user who uses the interface to define the interface from the interface of the interface. The practice of habits is to define the interface from the implementation class, that is, there is interface after implementation. I think it is more reasonable from customer departure definition interface. Let us consider the factors that cause interface changes. The interface will only be changed when the customer thinks it cannot meet the requirements, and does not change due to its implementation change. So the interface and the customer relationship use it is more closely. So, you should define the interface from the customer and name it to the interface. For example, a customer requires a storage module, which should define an ISToresevice interface indicating that it can provide a storage service instead of an IDB interface.

According to ISP (interface separation) principles, the interface should be separated using its customers, can not allow customers to rely on the interface that does not use it. This also shows that the interface is to define the interface from the customer. Such an interface is a highly inner parent-coupled.

2. First define the interface or write the specific class first?

I tend to define the interface first. First, start from the perspective of the customer to define the interface, then use the specific class to implement these interfaces. This is also an inevitable result from the customer to define the interface. Of course, some people think that this is excessive, and it is considered that the interface should be added when an interface should be required. This may analyze the decision according to the actual situation.

3. Can I only communicate between the objects?

Nor. If you look from unproced coupling and isolation, you don't need to define the interface.

1. If the relationship between the two classes is relatively tight, the implementation of the implementation is strong, and the definition interface is futile. Only complexity will only be increased.

2. If both classes are always changed, the interface between these two classes is also redundant because it is excessive.

3. For a very stable class, there is no need to define the interface. Since it doesn't change, what are we isolated? Most of this class is the basic class and tool classes in some class libraries.

Several other situations are dependent on the actual situation. There are still some principles to see "Interface and masks".

I know very well about the ideological understanding of the object, but ignorant is fearless, I hope this article can play the role of tiles. I hope everyone will discuss more communication.

转载请注明原文地址:https://www.9cbs.com/read-124374.html

New Post(0)