Carl.

http://community.borland.com/article/0,1410,22576,00.html

This article describes enforcing a singleton via the class itself which
Depending on what your singleton is doing may work better for you.

In essence it overrides
class function NewInstance: TObject; virtual;
procedure FreeInstance; virtual;

so that 
any time you call create on the class it actually returns the singleton
instance, and when you free it it dereferences. So you treat the class
as though each user of the class has its own memory managed instance,
but it will always point to the same one.

Regards
  K

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of Karl @ Work
Sent: Tuesday, 23 May 2006 4:40 p.m.
To: 'NZ Borland Developers Group - Delphi List'
Subject: RE: [DUG] Usage - initialization and finalization

> I'm curious as to why each of your consuming units needs to keep a
> reference to the singleton?
> 
> IMHO, surely the whole point about a singleton is that you don't keep
> references per se to it.
> 
> When you need to access the singleton, you should just call your
> Singleton function declared in your MySingletonUnit unit?
> 
> Surely that would result in a whole lot less reference counting, and
> worrying about who's holding on to it and when they drop their
> references, etc.

I would prefer not to keep references to the singleton in the consuming
units, it's just that I haven't found another way to ensure that it will
free itself both (a) after everything else has finished using it and (b)
before the program finishes.  The problem is that the singleton can't be
cleaned up in the finalization of its own unit because there are still
references to it which are accessed after that.

Cheers,
Carl

_______________________________________________
Delphi mailing list
[email protected]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to