?????? Henry's VB.NET trip (10) - when
???????????????????????????????? Han Rui?
Dafu did not tell me the difference between the interface and the abstraction class. When did you use the interface, when do you inherit it. I didn't eat it, I didn't eat it, I was pondering, this is not, I have finished dinner, I will rush up the building, one room is turned, and I find big Li everywhere.
After a big battle, this old brother talked about smiling in the office with a few colleagues, I immediately walked forward, and I took him to the computer next to it. "Big Li Ge, I really want to dizzy, since there is such a big development space in VB.NET, in the form of the abstract class, then what difference? When do you use the interface?"
Listening to me a series of questions, Da Li smiled and shook his head, patted my shoulder and said: "Little brother, not, you can't get it, actually, it is very experienced programming designer, when is the interface, when Head pain with abstract class. "
I am a doubtful weight. However, in turn, the old birds are unclear, and I don't have to be uneasy. what……
Dafu looked at the expression I suddenly showed a surprise, but he didn't pay attention to: "But this problem should we still have a better analysis, let us know more about the specific meaning of the interface and abstraction class."
"We have said, the abstract class is a class that cannot be instantiated. Abstract classes can be fully implemented, but more common is that partial implementation or does not implement, so that the universal function of inherited inheritance, it can provide Members that have been implemented, so you can use abstract classes to ensure a specific number of identical features, but you cannot do this with the interface. "
"The interface is a fully abstract member collection. Its members cannot be implemented when the interface is defined. We can think of it as an operation-defined contract, the implementation of the interface is completely left to developers. The difference between them, if Seriously analyze, there are still a lot: in VB.NET, the class can only be inherited from a base class, so if you want to use the abstract class as a set of categories, these classes must be inherited from that class. The interface is different. It not only uses a class or structure to implement multiple interfaces, but an interface can also have multiple implementations. "
"That is to say, are they differ in providing polymorphism?" I seem to understand what I have understood.
"This is an important difference. We can also consider the use of interfaces or abstractions from the perspective of polymorphism." Da Li agreed to my point, "If you expect multiple versions of the component, we should Create an abstract class. This is because abstract classes provide simple and easy ways to control component version. By updating the base class, all inheritance classes automatically update with the changes. This is the benefits, of course, it is also a problem, right? "Fragile base class") On the other hand, once the interface cannot be changed once it is created. If a new version of the interface is required, a new interface must be created. So, if the created function will be used in a wide range of all-term objects, then Use the interface. "
I thought, then Big Li said: "Can you say this, the abstract class is mainly used for close objects, and the interface is best suited for unrelated classes."
Big Li stretched out the thumbs: "Yes, the young man is very good! You think, I will tell you in the morning, to create a control, first to implement some interfaces to make the system to identify (see foreseeable details" Interface "). The contact between the various controls is not big. So, the foundation is mostly interface. However, we should pay attention to a point, when designing, if you want to provide universal in all implementations of the component The function is implemented. This is because the reason we have just said, the abstract class allows some implementation classes, and the interface does not contain any member's implementation. "" Hey, understand, the difference between them understands "I nodded silently.
"In addition, there is a universal design idea. If you want to design a small and concise function block, use the interface. If you want to design a large function unit, use the abstraction class." Da Li also added a suggestion.
"It seems that the problem is still quite big, in general, how to design an interface?" I asked.
"Why do you see the programming books, the program routine is good, very few have a description of the interface, and the example of inheriting the class is all compared to us? This will give us a wake up from one side, if Use appropriate, the interface can be a useful tool. But if you use improper use, they will become very tricky, even hinder effective programming. The design and use of the interface is actually a high level of art. "Da Li Zheng said .
"Art?" I screamed.
"Yes, art!" Da Li also added tone, "Through interface and implementation, we can use the same type of program to use the same type of object, and do not have to modify the original class, the relative subroutine must pass by modification Source Code can achieve reuse purposes, interface and implementation is not only great progress, but also the high programming art. "
"Oh, this is true." I recall the routine of the interface you see today.
"But the biggest problem is to focus on interface design." Big Li took, "once the interface is defined and accepted, it must remain unchanged to protect the application written in the interface. After the interface is released, It cannot be changed. This is an important principle of our component design, called 'interface invariance'. "
I nodded: "The interface does not change, this I can understand."
"I have repeatedly emphasized that creation an interface is to create a definition. After the interface definition is released, it will never be changed. Interface invariance is to protect the existing system written for the use of the interface. Demand for interface design and demand If you need to make a large change, we should create a new interface. The general naming method is that the new interface can be named by adding a number '2' after the original interface name to show it with existing interfaces. Relationship. Then use the interface inherit to create a new interface. "
"But if the demand is not good, don't you have a lot of derived interface?" I should not be a little concern.
"This is affirmative, and it is too frequently generated to make the components huge because of unused interfaces. Experienced designers, after the need to analyze the demand, the interface is often small and mutually Independence, reducing the possibility of performance problems. "
"This decomposition ability is really art!" I can't help but sigh.
"Of course, in general, we will determine which attributes and methods belong to the design process of the interface is called 'interface decomposition'. Basic guidelines are, should be closely related to several functions in an interface to a group. Function too There will be more convenient to run, and too segmentation features will result in additional system overhead and reduce the simplicity of use. The ability of this degree of decomposition is the need to constantly work in technology, and in depth on each project On the basis of the analysis and understanding. "" Understand. "I replied loudly, I really hope that I can become an interface design master.
Big Li smiled and patted me: "It is good. In fact, it is more likely to create a big realization inheritance tree compared to the design interface."
"Of course," I have an all aspect of the inheritance in my mind. "Is this not to say that the interface is still very beneficial to use the interface for some time."
"It seems that you really understand, then you will come again, the interface is inherited than the class, what is the benefits?" Da Li returned to the tea cup.
I turned down and turned my mind: "I tried it, overall, the interface is a very effective programming method, which allows the object to be separated from the implementation, so that there is no destruction of existing applications Under the case of the program, the object is perfect and evolved. The interface eliminates a big problem for the inheritance, which is likely to damage the code after changing the design implementation. Even if the inherit is allowed to inherit the class It will still make us a lot of choices for the design. If the original imagination is incorrect, it is not always possible to change the code in future versions. For example, we define a base class Method, it requires an Integer parameter, and later determines that the parameter should be a long data type. We can't change the original class because applications designed to be derived from the original class may not be able to compile. This issue Will expand because a single base class affects hundreds of subclasses. "
"Is it possible to solve this problem with the use of a long-loaded original class and use a long type?" Da Li made a problem.
"This?" I thought, "But this is not necessarily satisfactory, because a derived class may need to rewrite the method using an integer, if the method of taking the long data type cannot be rewritten, Detective classes may not function properly. "
"How to do it with the interface?" Da Li continued to ask.
"The way is to release updated interfaces that accept new data types." I replied at once.
"It seems that you have already mastered the basic links of interface operations." Da Li's comments really made me happy. "I will help you summarize it, using interface inheritance without having to inherit the main reason: in the application requires many object types that may not be related to provide some function, the interface applicability will be stronger; interface ratio The base class is more flexible because a single implementation can be defined to implement multiple interfaces; if the interface is not required to be implemented from the base class, the interface is better; if the interface cannot be used, the interface is very useful. For example, the structure cannot be Class inheritance, but they can implement interfaces. "
I took my mouth to nod, and I silently remember every criterion that Da Li said.
"Go back and think about it, write a few small programs to practice, tomorrow we have to appreciate the performance of VB.NET provided by the powerful visual inheritance."
(Endlessly)
-------------------------------------------------- -------------
Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.
QQ: 18349592
E-mail: Henry7685@hotmail.com