Delphi object-oriented programming 20 rules (by marco cantu) (Rule 16-20)

zhaozj2021-02-16  61

Rule 16: Visual Form Inheritance, if applied, this will be a powerful tool. According to my experience, the bigger the project you have developed, the more it can reflect its value. In a complex program, you can use the different level of relationship of the form to process a polymorphism of a group of related forms (Polymorphism). Visual form inherits allows you to share some public actions of multiple forms: You can use shared methods, common properties, even event handlers, components, component properties, component event processing methods, and more.

Rules 17: Limit Protected Data (Limit Protected Data) When you create some classes with different hierarchical systems, some programmers tend to primarily use protected domains because private data cannot be accessed by subclavab. I can't say this doesn't matter, but this is definitely incompatible with encapsulation. The implementation of the protection data can be shared by all inherited forms, and once the original definition of this data changes, you must change all related parts. Note that if you follow a rule such as hidden components (Rule 14), inheriting the form is impossible to access the private component of the base class. In a inheritance form, the code like edit1.text: = '' will not be compiled. Although this is quite inconvenient, at least in theory this is worthy of affirmation, not a negation. If you feel that the encapsulation is the most important, you need, please refer to the private segment of the base class.

Rules 18: Protected Access Methods in the base class in the base class in the private domain, and add some access functions to these components to get their properties, which will be a better way. . If these access functions are only used inside these classes and is not part of the class interface, you should declare them in the protection domain. For example, the GetText and SetText methods described in Rule 11 can be declared as protected and we can edit text by calling setText (''). In fact, when a method is mirrored to an attribute, we can simply use the following code to reach the editing text: Text: = '';

Rules 19: Protected Virtual Methods in the domain to implement a flexible hierarchical system is that some virtual methods you can call from external classes can be obtained. If this method is used, it will raise a situation in which other public methods call the virtual method in the protected domain. This is an important trick because you can customize the virtual method of the party to modify the action of the object.

Rule 20: Virtual Methods for Properties (Virtual Methods for Properties) Even if the method of access attribute can be defined as Virtual, the derived class can change the actions of the property without having to define them. Although this method is rarely used in VCL, it is indeed very flexible and powerful. In order to achieve this, just the need to define the GET and SET methods among Rule 11 as Virtual. The code of the base class is as follows:

type TformDialog = class (TForm) Procedure FormCreate (Sender: Tobject); Private Edit1: Tedit; Protected function GetText: String; virtual; procedure SetText (const Value: String); virtual; public constructor Create (Text: String): reintroduce; OverLoad; Property Text: String ReadText Write setText; End; In inheritance form, you can add some additional actions to overload the virtual method SetText:

Procedure tForminherit.Settext (const value: string); begin inherited settext (value); if value = '' Then Button1.enabled: = false;

Summary To do a good Delphi object-oriented programmer, the rule I mentioned above is so simple. There are some of these 20 rules that may require enough tolerance and time, so I can't force you to follow all of these rules. But these rules should be applied to your program by appropriate, and the more programmers who have developed, the more important participants, the more important these rules. However, even some small procedures, remember these rules and use them in the right place will also help you. Of course, there are many other experience rules I have not involved, especially memory handling and RTTI issues, because they are very complicated, requires special descriptions. My conclusion is to follow the rules I have listed above will pay a certain price, especially the extra code, but these costs will make you get a more flexible and strong program. I hope that the subsequent version of Delphi can help group we reduce these costs.

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

New Post(0)