Re: Singleton



Is this good or bad to make singleton in this way:

Type
TMySingleton = class
private

public


end;

function MySingleton: TMySingleton;

If you want to absolutely prevent even the attempt to create a second
instance, you can use an interface:

type
IMySingleton = interface
procedure MyMethod;
end;

function MySingleton: IMySingleton;

implementation

{$J+} //or just use a global variable (yuk...)

type
TMySingleton = class(TinterfacedObject, IMySingleton)
protected
procedure MyMethod;
end;

procedure TMySingleton.MyMethod;
begin
//blah
end;

function MySingleton: IMySingleton;
const
Obj: IMySingleton = nil; //note the type is the interface not the
class
begin
if Obj = nil then Obj := TMySingleton.Create;
Result := Obj;
end;

Note that this way does not require a finalization section to free the
allocated object.
.