[fpc-pascal] Implementing a true Singleton - Can we decrease the visibility of a method?
Vincent Snijders
vsnijders at quicknet.nl
Fri Dec 8 10:31:22 CET 2006
Graeme Geldenhuys schreef:
> On 12/8/06, Vincent Snijders <vsnijders at quicknet.nl> wrote:
>> Maybe I am analyzing your output wrong, but to me it seems as if the
>> contructor of
>> TObject is called in the third case. printdata seems to print an empty
>> text.
>>
>> So the protected contructor is unreachable, but the public constructor
>> in TObject
>> remains public.
>
> That's correct, which is even worse. It gives us a half-baked object.
> fData is never initialized. Putting a writeln() statement inside
> TSingleton.Create shows that on the 3rd instance it is never executed.
>
I think the solution is to create a public constructor which throws an
NotImplemented exception or something like that.
Then you create a protected (or private) constructor
constructor CreateSingletonInstance;
which is accessible only in that unit.
Note: instead of making function GetSingleton : TSingleton; a global function you
could make it class function GetInstance too.
Vincent
More information about the fpc-pascal
mailing list