[Lazarus] Best practices on using classes for subroutine libraries

Sven Barth pascaldragon at googlemail.com
Sat Apr 14 11:42:12 CEST 2012


On 14.04.2012 09:23, Frank Church wrote:
> On 10 April 2012 19:08, Marcos Douglas<md at delfire.net>  wrote:
>> On Tue, Apr 10, 2012 at 12:49 PM, Frank Church<vfclists at gmail.com>  wrote:
>>> On 10 April 2012 12:28, Marcos Douglas<md at delfire.net>  wrote:
>>>> On Tue, Apr 10, 2012 at 1:07 AM, Frank Church<vfclists at gmail.com>  wrote:
>>>>> When I need a variation in procedures (standalone, not a class or
>>>>> object procedure) I use across my applications, I often find it more
>>>>> convenient to create a copy the unit into the project folder and make
>>>>> the changes there. Sometimes they get merged back into the shared unit
>>>>> or not at all.
>>>>>
>>>>> I am thinking now of using classes with procedures, without any data
>>>>> in them, so that I subclass them in the projects where I need to
>>>>> change them. Are the some good examples and some known good working
>>>>> practices on working with procedures in this way?
>>>>
>>>> I do this. Sometimes I prefer encapsulate subroutines (LCL, RTL, 3rd
>>>> etc) in static classes.
>>>>
>>>> TmyFoo = class
>>>> public
>>>>   class procedure Exec; static;
>>>> end;
>>>>
>>>
>>> Do you have some examples to show how it is done?
>>>
>>> Are there some examples in the FCL/LCL etc
>>
>> In FCL/LCL I don't know if exists.
>>
>> My example:
>>
>> type
>>   TMsg = class
>>   public
>>     class procedure Info(const ATxt: string); static;
>>   end;
>>
>> implementation
>>
>> class procedure TMsg.Info(const ATxt: string);
>> begin
>>   ShowMessage(ATxt);
>> end;
>>
>> ------------------------
>> Using:
>>
>> begin
>>   TMsg.Info;
>> end;
>>
>
> I see that you declare your procedure as static. Is that the way they
> should be or is that your own preference?
> Is it possible to make them virtual and override them or replace them
> in descendant classes?

I'd say it is his own preference. Static methods are like normal, global 
functions/procedures except they are located inside classes. You can 
even pass them to non-object procedure variables. If you want to 
override them you should leave out the "static" and declare them 
"virtual" (this works with class methods as well as with normal methods).

Regards,
Sven





More information about the Lazarus mailing list