Re: Singleton
- From: "Chris Rolliston" <spam@xxxxxxxxx>
- Date: 15 Jan 2008 17:42:26 -0700
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.
.
- Follow-Ups:
- Re: Singleton
- From: Chris Morgan
- Re: Singleton
- From: Jens Mühlenhoff
- Re: Singleton
- From: Jens Mühlenhoff
- Re: Singleton
- References:
- Singleton
- From: Alan T
- Singleton
- Prev by Date: Singleton
- Next by Date: Re: EU is on the loose.
- Previous by thread: Re: Singleton
- Next by thread: Re: Singleton
- Index(es):