[fpc-pascal] Implementing a true Singleton - Can we decrease	the	visibility of a method?
    Vincent Snijders 
    vsnijders at quicknet.nl
       
    Fri Dec  8 14:15:32 CET 2006
    
    
  
Graeme Geldenhuys schreef:
> On 12/8/06, Jonas Maebe <jonas.maebe at elis.ugent.be> wrote:
>> Quote:
>>
>> "You should not change the access modifier for inherited members."
>>
>> So it's possible but discouraged, and they give warnings (and
>> sometimes errors) for it with the recommendation "Do not exclude a
>> warning from this rule."
> 
> There is a lot of things in life we shouldn't do, but we do.  :-)
> At least give us the choice, maybe via a compiler directive and keep
> the compiler warning in place.  That way we can use it responsibly
> when required, as in the case of implementing a true singleton. I'm
> sure there are more examples. As it stands currently (my sample
> singleton code) the developer can still screw up by creating a new
> instance instead of going through the global function.
> 
I think it cannot be prevented.
Suppose you could hide the constructor and TSingleton.Create was not 
visible, I still could create a TSingleton object and call the 
TObject.Create on it.
var
   Singleton1, Singleton2: TSingleton;
begin
   Singleton1 := TSingleton.CreateInstance;
   TObject(Singleton1).Create;
   Singleton2 := TSingleton.CreateInstance;
   TObject(Singleton).Create;
   // now there are two Singleton objects.
end.
    
    
More information about the fpc-pascal
mailing list