Henry VB.NET Tour (Seven) - Fragile Base
Han Rui
"Since it is fragile, of course, it means that it is as unbearable as the egg shell. This problem is actually very good. The program is always designed and written by people, so the work is not considered for some problems. Of course, it is also very Normal things. So it is possible to find that the base class needs to be changed for a while. You want, if I change the data type of the member in the base class, and those methods and properties that allow rewriting, that distributes and Can their subclasses work normally? Especially when multiple developers in a team come to create base classes and derived classes, it is more important. In many cases, everyone may have already put the base class and some derived classes. Compiled into a binary form for submission. Change the base class, recompilation and then distributed, will move a whole body, lead to the collapse of the project. So we call this 'fragile base class'. That is, it is the entire project The weak and most deadly links. "Da Li's brow has been locked, and it is necessary to recall the experience of being hit.
"So serious, the current software engineering design method will not have a good solution to this?" I strive to relieve the serious look of Da Li.
"If the project's pre-design consideration is as detailed, the code control and correlation analysis of the project in the project implementation will play a good effect. But no matter how people work, sometimes it is still unable to avoid the basis The class is unpredictable. We have been exploited for a long time, with some means of processing. "
"It's really a thing, what do we have any solution now?" I also looked up.
"Oh, it is not a perfect solution. It can only be reduced to some extent. We are often used for a method, the most direct thinking is to put all possible changes in the derived class, not in the base class do."
"What does this mean, I still don't quite understand." I am embarrassed to scratch the head.
Da Li smiled nodded, it seems to know that I won't understand. "We use the abstraction class in the base class. It is only defined by the method and attributes, and does not implement, and put the implementation part is in the derived class. This way, the abstract class itself cannot be instantiated However, its benefits are self-evolved, that is, the implementation of the implementation will only involve its derived class. VB.NET provides such a means. "
Said, Da Li opened the vs.net integrated compilation environment, wrote a small code in hand:
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 subs (Byval X as integer)
??????? 'Write the code implemented
??? End Sub
??? public overrides Function Fcny (Byval Y AS Integer) AS LONG
??????? 'write the code implemented
??? End function
END CLASS
?
"Here to pay attention to two problems, one is keyword, we use Mustinherit to modify class names, making the class into an abstract class, in its member, adding the method and attribute to the MustoverRide modifier to indicate that they must be in the derived class Realization. The second thing to note is that the derived class must be implemented on all basic class methods and properties identified by the Mustoverride, and only rewrite the SUBX, and the Fcny compiler will be reported. "
"This can indeed solve some of the problems, but it seems that only the code that implements the code in the base class has changed. For the change of the data type, there is no effect." I saw it for a while, I sent this. doubt. "So I just said that it is solved some extent." Da Li also couldn't help but laugh, "But you will not be too much trouble, we can use shadows in derived class!" (For details, please refer to the previous issue of this newspaper "overload and hidden") "
This is a good idea, my heart is secretly evaluated. Suddenly I thought of a question: "If the base class wants to do a functional extension, what should I do?"
"If you want to do extension, the safest way is to add new members, not a big modification of the base class. Generally, new members who are derived when designing design. It is best not to use OverLoads keywords to name and base classes. The same members, often bring you unexpected questions. It is best to redefine new members, and you must try to distinguish from the members of the base class existing member name. In fact, you can also add it to the abstract class base class. The definition of a new member, but in this way, it is necessary to set the version for the base class. Although it will not cause devastating hazards for the application, it should be able to fully control and manage your own code. We generally do not want to expand the base class. of."
I have already gotten up the great efforts of Da Li: "What you mean, is it, it is actually an objective existence, what we do is to minimize this problem? "
The smile of a thumbs up in Da Li, "Yes, for an application-oriented designer, you want to use object-oriented methods to develop, you must carefully plan the hierarchy of class. In general, there are several guidelines that need to be grasped:
First, follow the principles of general, re-dedicated. Firstly design the general part of each level in the hierarchy, which is a member of the derived class inheritance and a member identified as public;
Second, there is a reservation amount when defining data types and storage areas to avoid changes in future changes. For example, even if the current data may only require an Integer type, we use the loop variable when designing. Of course, it is best to do it, or blindly enlarged;
Third, in a project, you must unify all names used in the assignment team to reduce naming conflicts, this is actually important;
Fourth, to use access modifiers that provide a feasible minimum access to declare class members. Internal group members should be declared as private; members only need to be derived only within the class should be marked as protected; Friend data members can access external access from classes, but only for this module is an integral part of the item defined. Members using the public identity can only be realized when instantiated, and often used at the bottom of the class hierarchy. "
"That is to say, a standardized operation, the standard naming system can determine the strongness of the base class?" I can't help but touch it.
"No, it should be said that it can be decided to put a thick protective clothing to the fragile base class. Because the base class is always fragile." Da Li made a way.
I even endorsed: "Yes, right. I am really understanding why there is a matter of someone to make programming standards. I have always thought that it is the readability of the code, I didn't expect it to help the program itself."
"Of course, in fact, you carefully think about the role of Overrides keywords, whether or not, the compiler can easily determine if the method or attribute is in the base class, but VB.NET requires us to be marked It is to force developers to indicate the intent of the heavy-duty base class or attribute, so that the development process is more reasonable and effective. In addition, more importantly, we must constantly learn and hone in engineering practice, learn more Knowledge, get more experience, so that it will grow into a qualified programming designer. Take the inheritance, in the .NET actually support three inheritance methods: achieve inheritance, interface inheritance, visual inheritance. We actually only use the first inheritance mode. Do you think, is it a lot of things to learn? "Big Li took a shot my shoulder. (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
Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/