Re: Singleton & Self := in constructor

From: Peter Below (TeamB) (100113.1101_at_compuXXserve.com)
Date: 01/04/04


Date: Sun, 04 Jan 2004 20:55:03 +0100

In article <3ff0abb7$1@newsgroups.borland.com>, Sal wrote:
> I want to enforce singleton behavior in my TObject derived
> class. I'd just like to know if this approach will work.
> The basic idea is that clients can call the Constructor any
> number of times, but will always get the same singleton
> instance as a result.
>
> ///////////////////
>
> Unit MySingleton
>
> interface
>
> Uses Classes;
>
> type
> TMySingleton = class
> private
> public
> Constructor Create;
> end;
>
> implementation
>
> var
>
> Instance: TMySingleton;
>
> // TMySingleton
>
> Constructor TMySingleton.Create;
> begin
> if Instance <> Nil then
> Self := Instance
> else
> begin
> Inherited Create;
> Instance := Self;
> end;
> end;

Just for the record: this does not work. When the code flow enters your
create the instance of the class has already been created, its memory
has been allocated and initialized. So you are leaking memory like mad
here <g>. There is also the side effect that the classes
AfterConstruction method will be called on every invokation of the
constructor.

You usually need to override NewInstance and FreeInstance to implement
a singleton class. But it is much simpler to just use an interface
instead of a class. Only the interface is public and the unit contains
a factory function that returns an interface reference of this type.
The Implementation contains an Instance variable like yours, but it is
of the interface type, not the object type that implements the
interface. The object descends from TInterfaceObject, of course.

The automatic reference counting gives you singleton behaviour for
free. Of course there is a way to kill the singleton by calling
_Release on the interface reference until it is dead, but that falls
under malicious coding practices, IMO <g>.

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


Relevant Pages

  • Re: Singleton
    ... hide the singleton class wholesale in the implementation section of the ... The interface is of course easily replaced with an abstract class: ... property MyFirstProperty: Integer read GetMyFirstProperty write ... procedure MyFirstProcedure; virtual; abstract; ...
    (borland.public.delphi.non-technical)
  • Re: Windows Service in VB
    ... Your service needs to expose an interface via .Net remoting. ... and register the public class as a well-known singleton. ... You also need to create a WinForms app that can be run by logged-in users. ... if your client app just wants to call methods on the service's remoted ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Singleton
    ... I don't usually go to a string factory or a list factory to ... If you have an abstract DOM interface, for example, and the implementation ... To me it's very clear to go to the DomFactory or singleton ... As mentioned in my posting you unit test a class separate from the ...
    (comp.object)
  • Re: Factory und Interface zusammenpacken?
    ... Bisher habe ich es so gemacht dass ich ein Interface habe, ... weitere Klasse, eine Factory-Method-Klasse, über die ich jetzt eine der ... Dein Problem ist, dass du eine einheitliche Struktur brauchst, um die Singleton Instanzen zu erzeugen, wenn ich das richtig verstanden habe, oder? ... class DBConfig implements Config, Singleton ...
    (de.comp.objekt)
  • Re: Singleton & Interfaces
    ... Basically, since both singletons expose the same interface, you ... public static class A: IMyInterface ... If you were truly implementing the singleton pattern, ...
    (microsoft.public.dotnet.languages.csharp)