Use classhelper/record helper to "patch" the class"

Source: Internet
Author: User

Sometimes, we need to add something to the class based on the existing class.
In addition, the protected differentiation method in the class cannot be accessed by members outside the unit. What should we do if we want to call them?
The following three methods are available:

1. inherit this class
2. Directly override this class
3. Use class helper

Everyone is familiar with the first method. I will not talk about it. The implementation of the second method:

Tcontrol = Class (controls. tcontrol)
Private
Ftext: widestring;
Published
Property text: widestring read ftext write ftext;
End;

The third type is what I will talk about in this article. classhelper can be used to expand the class. It can also be called "Patching" the class"

The specific operation method is as follows. Suppose there is a very simple class:
Type
Ttestclass = Class
Private
Finfo: string;
Protected
Procedure dotest;
Public
Function doadd (A, B: integer): string;
Public
Property info: String read finfo write finfo;
End;

Now I want to add a dominus method to it, so I have this class helper

Type
Ttestclasshelper = Class helper for ttestclass
Public
Function Dominus (A, B: integer): string;
End;

As you can see, it's just easy to declare a class helper and then add content to it.
The advantage of doing so is that the original class code will not be changed, but it will only be extended
In class helper, you can access the members, attributes, and methods of the base class's protected, public, and published delimiters, but cannot access those in private.

After the patch of class helper, we can call the added method under the base class instance.
VaR
T: ttestclass;
Begin
T: = ttestclass. Create;
T. doadd (1, 2 );
T. Dominus (2, 1 );
End;

This is just a small example. I have seen that class helper is very complex. It has strong concealment for code
For example:
Type
Ttestclass = Class
Protected
Fa, FB, FC: string;
Public
End;

Type
Ttestclasshelper = Class helper for ttestclass
Public
Procedure do1;
Procedure do2;
......
End;

This kind of code is really a headache. You can only see one empty class by pressing CTRL and there is no method.
All the implemented code is put in class helper, and class helper is not written in PAS, but written in INC after being split.

Summary
Class helper is useful for simple class patching. After all, writing a class helper is similar to inheriting a class, and class helper does not affect the base class, you don't have to worry about overwriting the base class accidentally.
However, class helper is not suitable for large-scale class rewriting. In particular, using class helper to implement semi-open-source products is not conducive to code maintenance (this is not the case if it is a commercial method ), in this case, a detailed document is required to identify the class helper and the class it refers.

 

Originally, in order to be compatible with the units compiled by d2006, Delphi 2007 thought of using class helper. However, class helper/record helper is only suitable for "patches" and should not be placed in regular designs.

The record helper is used to expand the tguid structure, which is very convenient.

Type
Tguidhelper = record helper for tguid
Private
Class function getempty: tguid; static;
Public
Class function create (const G: string): tguid; overload; static;
Class function newguid: tguid; static;
Function equals (const guid: tguid): Boolean;
Function tostring: string;
Class property empty: tguid read getempty;
End;

 

 

Http://blog.sina.com.cn/s/blog_6016bdc80100dhbh.html ~ Type = v5_one & label = rela_prevarticle? 1290186141

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.