Henry's VB. NET journey (10)-When to use interfaces

Source: Internet
Author: User

?????? Henry's VB. NET journey (10)-When to use interfaces

??????????????????????????????????????? Han Rui?

Li did not tell me the difference between an interface and an abstract class, when to use an interface, and when to use implement inheritance. I have never had a good meal, and I am always thinking about it. No, once I have finished, I will rush upstairs, switch to a room, and look for Da li everywhere.

After a while, this old brother and a few colleagues came back to the office with a smile. I immediately stepped forward and dragged him to the computer. "Big Brother Li, I really felt dizzy. Since interfaces in VB. NET have such a huge space for development and are similar in form to abstract classes, what are the differences between them? When should I use interfaces ?"

After listening to a series of questions, da Li shook his head with a smile and patted me on the shoulder and said, "little brother, not only are you confused, but it is actually very experienced programmers who also use interfaces when, it is a headache to use abstract classes."

In this case, I am even more confused. However, the old birds are confused and I don't have to worry about it. Haha ......

David looked at me with a strange look, but he ignored it and continued: "But we still need to analyze this problem, let us better understand the specific meaning of interfaces and abstract classes."

"We have said that abstract classes are classes that must be inherited from them but cannot be instantiated. Abstract classes can be fully implemented, but more commonly, they are partially implemented or not implemented at all, so as to encapsulate the general functions of the inherited class. It can provide the implemented members. Therefore, abstract classes can be used to ensure that a specified number of functions are the same, but interfaces cannot be used to do so."

"An interface is a fully abstract set of members, and its members cannot be implemented when the interface is defined. We can regard it as a contract for Operation definition, and the implementation of the interface is left for developers to do. The difference between them, if carefully analyzed, is still quite a lot: In VB. in. NET, classes can only be inherited from a base class, so if you want to use abstract classes to provide polymorphism for a group of classes, these classes must all be inherited from that class; interfaces are different, it can not only implement multiple interfaces with one class or structure, but also multiple implementations of one interface."

"That is to say, are there any differences between them on the issue of providing polymorphism ?" I seem to understand something.

"This is an important difference. We can also consider whether to use interfaces or abstract classes from the perspective of polymorphism ." David agrees with me: "If you want to create multiple versions of a component, you should create an abstract class. This is because abstract classes provide simple methods to control component versions. By updating the base class, all inheritance classes are automatically updated with the change. This is a benefit, but it is also a problem, right? (For details, see the fragile base class.) On the other hand, the interface cannot be changed once it is created. If you need a new version of the interface, you must create a new interface. Therefore, if the created function is used across a wide range of different objects, the interface is used ."

I thought for a moment and then said, "can you say this? abstract classes are mainly used for closely related objects, and interfaces are most suitable for providing general functions for irrelevant classes ."

Big Li gave me a thumbs up: "Good, good boy! You think, I told you in the morning that to create a control, you must first implement some interfaces to enable the system to recognize them (see interface in the previous article). In fact, the relationship between various controls is not highly relevant. Therefore, most of them are interfaces. However, we should note that when designing a component, an abstract class is used to provide common implemented functions among all the implementations of the component. This is because the abstract class allows partial implementation classes, and the interface does not contain any member implementations ."

"Well, I understand the differences between them ." I nodded silently.

"In addition, there is a general design concept. If you want to design small and concise functional blocks, you can use interfaces. If you want to design a large functional unit, use an abstract class ." Da li added another suggestion.

"It seems that the design problem is still quite big. In general, how to design interfaces ?" I went on to ask.

"Why do you see Programming books, program routines, and very few descriptions of interfaces, and examples of class implementation inheritance are everywhere? This reminds us of one aspect. If appropriate, interfaces can become very useful tools. However, if improperly used, they become very tricky and even impede effective programming. The design and use of interfaces is actually a brilliant art ." Said Da li seriously.

"Art ?" I was surprised.

"That's right, Art !" Da li added his tone: "Through interfaces and implementation, we can apply the same type of program to different objects without modifying the original class, compared with subprograms, you must modify the source code to achieve reuse. interfaces and implementations are not only great progress, but also an extremely high level of programming art."

"Oh, that's true ." I recall the interface routine I saw today.

"However, the biggest problem is the interface design ." "Once an interface is defined and accepted, it must remain unchanged to protect applications written to use the interface. After the API is released, it cannot be changed. This is an important principle for component design. It is called 'interface immutability '."

I nodded: "The interface is immutable. I can understand this ."

"I have already repeatedly stressed that creating an interface is to create a definition, which cannot be changed once released. The immutability of interfaces is to protect existing systems written to use interfaces. When there are differences between the interface design and requirements and the need for significant changes, we should create a new interface. The general naming method is that the new interface can be named by appending a number '2' after the original interface name to show its relationship with the existing interface. Then create an interface through interface inheritance ."

"However, if the demand analysis is poor, wouldn't there be a lot of derivative interfaces ?" I have some concerns.

"This is for sure, and too frequently new interfaces are generated, which makes the components very large because of unused interface implementations. Experienced designers, after fully analyzing the requirements, the interfaces designed are often very small and independent from each other, reducing the possibility of performance problems ."

"This decomposition capability is really art !" I can't help but sigh for it.

"Of course, in general, we call the design process of determining which attributes and Methods belong to an interface as 'interface decomposition '. The basic principle is to aggregate closely related functions into a group in an interface. Too many functions make the interface not easy to run, and too many subdivided functions will lead to additional system overhead and reduce ease of use. The ability to master the degree of decomposition requires constant technical refining and in-depth analysis and understanding of each project ."

"Understand ." I replied loudly, hoping that I could become an interface design master one day earlier.

David smiled and patted me: "It's good to understand. In fact, it is easier to create a large implementation inheritance tree than to design interfaces ."

"Of course," all aspects of implementation inheritance appear in my mind. "This is not to say that it is helpful to use interfaces in some cases ."

"It seems that you really understand, so let's talk about the advantages of interfaces compared with class implementation inheritance ?" Big Li turned back and began to look for a cup.

I lowered my head and tried to turn my mind: "I'll try to talk about it. In general, interfaces are a very effective programming method that separates object definition and implementation, so that objects can be improved and evolved without disrupting existing applications. The interface eliminates a major problem of implementation inheritance, that is, the Code may be damaged when the design is modified. Even if the implementation inheritance allows the class to inherit from the base class, we still have to make a lot of choices for the design at the first release of the class. If the original idea is incorrect, it is not always possible to make security changes to the Code in a later version. For example, we defined a base class method, which requires an Integer parameter, and later confirmed that the parameter should be of the Long data type. We cannot safely change the original class because the applications designed for Classes derived from the original class may not be able to compile correctly. This problem will be extended because a single base class will affect hundreds of sub-classes ."

"Can this problem be solved if the original class is reloaded and a Long parameter is used ?" David raised a question.

"This ?" I thought for a moment, "however, this may not necessarily achieve satisfactory results, because a derived class may need to override the integer method.LongThe method of the data type cannot be overwritten, and the derived class may not run properly ."

"How to Use interfaces ?" David continued to ask.

"The solution is to publish an interface for accepting new data types ." I answered it all at once.

"It seems that you have mastered the basic steps of interface operations ." I am so happy with the comments from Da li. "I would like to sum up that the main reasons for using interface inheritance instead of class inheritance are: when the application requires a lot of irrelevant object types to provide some function, the interface is more adaptable. The interface is more flexible than the base class, because you can define a single implementation to implement multiple interfaces. The interface is better without inheriting the implementation from the base class; the interface is useful when class inheritance cannot be used. For example, structures cannot be inherited from classes, but they can implement interfaces ."

I nodded with a bid, and silently remembered every rule David said.

"Go back and think about it. Write a few more small programs to practice. Tomorrow we will also appreciate the powerful visual inheritance provided by VB. NET ."

(To be continued)

---------------------------------------------------------------

Disclaimer: the copyright and interpretation rights of this article are owned by Han Rui. If you need to reprint it, please keep the complete content and this statement.

QQ: 18349592

E-mail: henry7685@hotmail.com

Please visit my column: http://www.csdn.net/develop/author/netauthor/Latitude/

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.