Re: Singleton



Jens Mühlenhoff wrote:

type
TSingleton = class sealed(TObject) // sealed prevents the constructor check to be circumvented
private
FMyFirstProperty: Integer;
class var FInstance: TSingleton;
public
property MyFirstProperty: Integer read FMyFirstProperty write FMyFirstProperty;
constructor Create; // fires an exception when FInstance <> NIL
procedure MyFirstProcedure;
end;

None of this solutions is perfect, so YMMV.

I thought about that a little longer, maybe the Delphi language should add support for a new "singleton" keyword like this:

type
MySingleton = singleton
strice private
FMyFirstProperty: Integer;
public
property MyFirstProperty: Integer read FMyFirstProperty write
FMyFirstProperty;
procedure MyFirstProcedure;
end;

There should be no support for instantiation. You access this singleton by its type name:

MySingleton.MyFirstProcedure;

The only problem that is left is the initialization / finalization issue. I can't think of a clean solution that doesn't involve reference counting or a garbage collector.

--
Regards
Jens
.