Board index » delphi » Static class members. singletons

Static class members. singletons

I want to be able to create only one single copy of an object
In C++ this can be done like

// example from Design Patterns, "Elements od Reusable Software Design"
class singleton
{
public
  static Singleton* Instance();
protected
  Singletion()
private
  static Singleton* _single

Quote
}

Singleton* Singletong::_single = 0;

Singleton* Singleton::Instance() {
 if (_single == 0) {
  _instance = new Singleton;
 }
 return _instance

Quote
}

I'm looking for a comparable method in Delphi

Any suggestions????

--
Paul Sjoerdsma, software developer / designer
The Global Aquaculture Network
s...@ksc.net.th

 

Re:Static class members. singletons


In article <MPLANET.321f93a1snr989...@news.au.ac.th>, s...@ksc.net.th says...

Quote
> I want to be able to create only one single copy of an object
> In C++ this can be done like

> ...

> I'm looking for a comparable method in Delphi

Well...there is no equivalent to the singleton directive in C++ which enforces
that only one instance of a class exists.  So you can't prevent a programmer
using your class from creating additional instances of it.

If, however, you want to guarantee that an instance of the class is always
present, do this:

type

TMyObj = class
   ...
end;

var
   ObjInst : TMyObj;

implementation
...

initialization
   ObjInst := TMyObj.Create;
end.
----------------------------------------------------------
Dylan Steinberg                         dy...@voicenet.com
Integrated Systems Consulting Group     stein...@iscg.com

Re:Static class members. singletons


Quote
Dylan Steinberg wrote:

> In article <MPLANET.321f93a1snr989...@news.au.ac.th>, s...@ksc.net.th says...
> > I want to be able to create only one single copy of an object
> > In C++ this can be done like

> > ...

> > I'm looking for a comparable method in Delphi

> Well...there is no equivalent to the singleton directive in C++ which enforces
> that only one instance of a class exists.  So you can't prevent a programmer
> using your class from creating additional instances of it.

You could have the creator as private, and initialize it the way you
mention.

nst : TMyObj;

Quote

> implementation
> ...

> initialization
>    ObjInst := TMyObj.Create;
> end.

 _
| | |  _     _        http://www.stud.unit.no/~olavb/
|_| |_|_|_|_|                            Ol...@dds.nl
Eyecom Images, Amsterdam    finger:Ol...@stud.unit.no

Re:Static class members. singletons


Quote
>I want to be able to create only one single copy of an object
>In C++ this can be done like

The trick is in using the class keyword to designate "static" methods. I am
not sure if this is 100% correct, because I don't have the compiler here,
but it is close

type TSingleton = class
        public
                class function Instance : TSingleton;
        private
                class single : TSingleton;
end;

function TSingleton.Instance;
begin
        if single = nil then
                single := TSingleton.Create;
        Result := single;
end;

(* variables should be initialized to nil, but in any case *)

initialization
        TSingleton.single := nil;
end.

Re:Static class members. singletons


Quote
ROLAND KAUFMANN wrote:

> >I want to be able to create only one single copy of an object
> >In C++ this can be done like

> The trick is in using the class keyword to designate "static" methods. I am
> not sure if this is 100% correct, because I don't have the compiler here,
> but it is close

> type TSingleton = class
>         public
>                 class function Instance : TSingleton;
>         private
>                 class single : TSingleton;
> end;
> function TSingleton.Instance;
> begin
>         if single = nil then
>                 single := TSingleton.Create;
>         Result := single;
> end;
> (* variables should be initialized to nil, but in any case *)
> initialization
>         TSingleton.single := nil;
> end.

You cannot have class fields in Delphi, only class functions and procedures.
To get around this, use a global variable declared in the implementation
section. This makes it private to the unit where the class is defined.

As Olav mentions in another message, declare the constructor as private
to disable unauthorized creation of multiple instances:

interface
  TSingleton = class
  private
    contructor Create;
  public
    class function Instance: TSingleton;
    {Other public methods declared here }
  end;

implementation

var
  SingleInstance: TSingleton;

class function TSingleton.Instance: TSingleton;
begin
  if not Assigned(SingleInstance) then
    SingleInstance := TSingleton.Create;
  Result := SingleInstance;
end;

constructor TSingleton.Create;
begin
  inherited Create;
  {do init-stuff}
end;

initialization
  SingleInstance := nil;
end.

Whether to use a class method, normal function or an interfaced variable to
expose the single instance is a matter of taste. However this version
guarantees that none can mess with the pointer.

--
Hallvard Vassbotn

Re:Static class members. singletons


Quote
>> I'm looking for a comparable method in Delphi

>Well...there is no equivalent to the singleton directive in C++ which enforces
>that only one instance of a class exists.  So you can't prevent a programmer
>using your class from creating additional instances of it.

        You can declare a private variable, increment it in the constructor. If it is 1
upon entry, generate an exception on the constructor.

_
NOTE: This software is currently in early alpha. If you notice any
problems, or RFC non-compliance, please report it to p...@pbe.com
 \------------------------------------------------------------\
  \           Chad Z. Hower  -  phoe...@pobox.com              \
   \  Phoenix Business Enterprises - p...@pbe.com - www.pbe.com  \
    \     Physically in Church Hill, TN - Logically Not Sure     \
     \------------------------------------------------------------\

Quote
>>SQUID - The ultimate 95/NT offline databasing reader

**Special Compile: 3.000A (Alpha)

Other Threads