Henry's VB. NET journey (7)-vulnerable base class

Source: Internet
Author: User

Henry's VB. NET journey (7)-vulnerable base class

Han Rui

 

"Since it is fragile, it means it is as vulnerable as an eggshell. This problem is actually very understandable. The program is always designed and written by people, so it is normal that some problems are not considered at the beginning of work. Therefore, after a period of work, you may find that the base class needs to be changed. If I have changed the member data type in the base class and the methods and attributes that can be rewritten, can the derived class and its subclass work normally? Especially when multiple developers in a team create a base class and a derived class together, it is even worse. In many cases, you may have compiled the base class and some Derived classes into binary form for submission. Changing the base class and re-Compiling and re-distribution will lead to a full-body crash. So we call this 'vulnerable base class '. That is to say, it is the weakest and most fatal link in the whole project ." Big Li's brow has been locked, presumably recalling his own experiences.

"So serious, will the current software engineering design method provide a good solution for this ?" I try to ease the serious look of Da li.

"If you consider the preliminary design of the project as much as possible, the project code control and correlation analysis in the project implementation will be very effective. However, no matter how hard a person works, it is sometimes impossible to avoid unpredictable changes to the base class. We have been exploring for a long time and have some processing methods ."

"It's really a matter of fact. What can we do now ?" I was also excited.

"Haha, it's not a perfect solution. It can only mitigate the harm to some extent. One of our commonly used methods is to put all possible changes in the derived class rather than in the base class ."

"What does this mean? I still don't quite understand it ." I'm sorry to scratch my head.

David nodded with a smile. It seems that I can't understand it. "The abstract class is used in the base class. The methods and attributes contained in the abstract class are defined and not implemented. The implementation part is put in the derived class. In this way, the abstract class itself cannot be instantiated. But its advantages are self-evident, that is, any possible implementation changes will only involve its derived classes. VB. NET provides such a method ."

As said, Da li opened the VS. NET integrated compilation environment and wrote a short piece of code:

Public MustInherit Class CBaseHenry

??? Public MustOverride Sub subX (ByVal x As Integer)

??? Public MustOverride Function fcnY (ByVal y As Integer) As Long

End Class

Public Class CDerivedHenry

??? Inherits CBaseHenry

??? Public Overrides Sub subX (ByVal x As Integer)

?????? ? 'Write implementation code

??? End Sub

??? Public Overrides Function fcnY (ByVal y As Integer) As Long

??????? 'Write implementation code

??? End Function

End Class

?

"Pay attention to two issues here. One is a keyword. We useMustInheritClass Name To make the class an abstract class.MustOverrideModifiers indicate that they must be implemented in the derived class. The second thing to note is that the derived class must beMustOverrideThe basic class methods and attributes of the identifier are implemented. Only subX is overwritten. If the fcnY compiler is not written, an error is returned ."

"This can indeed solve some problems, but it seems that it can only solve the problem that the Code implemented in the base class has been changed, and the changes to the data type seem to have no effect ." After reading it for a while, I raised such questions.

"So I just said, to some extent, to solve it ." Da li couldn't help but smile. "However, it's not too troublesome for you to raise this question. We can use it in a derived class.ShadowsTo solve it! (For details, refer to the previous issue "overloading and hiding" in this document.)"

This is a good idea, and I secretly commented on it. Suddenly I came up with another question: "What if the base class needs to be expanded ?"

"The safest way to scale is to add new members, rather than making major changes to the base class. It is generally used to add a new member that is missing during design to the derived class. Not recommendedOverloadsKeyword to name the same member as the base class, which often brings unexpected problems. It is best to redefine new members and distinguish them from existing Member names of the base class as much as possible. In fact, you can also add a new member definition to the base class of the abstract class, but in this way, you need to develop a version for the base class, although it will not cause devastating harm to the application, however, you should be able to fully control and manage your own code. We generally do not want to extend the base class ."

I have understood the painstaking efforts of Da li: "Do you mean that the basic vulnerabilities actually exist objectively, what we do is to minimize the harm this problem brings?"

David smiled with approval in his eyes and said, "That's right. For an Application Designer, I want to use the object-oriented method for development, you must carefully plan the hierarchy of classes during design. Generally, there are several rules to grasp:

First, follow the general and dedicated principles. First, design the general part of the class at each level in the hierarchy, that is, to provide the inherited members and identifiers of the derived classPublic;

Second, there must be a reserved amount when defining data types and storage areas to avoid future changes. For example, even if the current data only needsIntegerThe type is enough. We use it during design.LongType variable. Of course, it is best to make the best use of things, and do not blindly enlarge;

Third, it is important to manage and assign all the names used by the team in a project to reduce naming conflicts;

Fourth, use an access modifier that provides feasible minimum access permissions to declare class members. Internal class members should be declaredPrivate; The members required only within the class and the derived class should be markedProtected;FriendData members can access from outside the class, but only this module is an integral part of the project that defines the class.PublicThe member of the identifier can only be the content actually needed during instantiation, and is often used at the bottom of the class hierarchy ."

"That is to say, a standard naming system can determine whether the base class is strong or not ?" I couldn't help but feel it.

"No, it should be said that we can decide how thick protective clothing the vulnerable base class should be. Because the base classes are always fragile ." Da li is more upright.

I repeatedly agreed: "Yes, yes. I really understand why some people always raise programming standards. I always think it is to enhance the readability of the code. I didn't expect it to be so helpful to the program itself ."

"Of course, you actually think about it seriously,OverridesThe compiler can easily determine whether the method or attribute is in the base class and whether the signature matches. NET requires that we must mark, that is, to force developers to indicate the intention to reload the basic class methods or attributes, so that the development process is more reasonable and effective. In addition, it is more important for us to continuously learn and hone in engineering practices, learn more, and gain more experience, in this way, it will grow into a qualified Program designer. Take inheritance as an example. NET supports three inheritance Methods: Implementation inheritance, interface inheritance, and visual inheritance. We actually only use the first inheritance method. Do you think there are many things to learn ?" Lee patted me on the shoulder.

(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.