tracking instances is a little trickier. we must:
- keep track of each existing instance of our class
- keep track of how many references there are to this instance
- create a new object only when no instance exists
- destroy the object when the last reference is removed
instance so we know what it refers to.
as for keeping track of how many references exist, we need another variable. we can it inside the class or make it a sort-of global like instance. i'll opt for the latter way but do what you feel is best. i'll name this variable ref_count.
we now have two variables:
var instance : tsingleton = nil; ref_count : integer = 0;i initialize the variables so that initially they don't contain any garbage. i know that the compiler does this automatically, so this is just a readability issue.
we'll need to declare the tsingleton class above the variable block, and if you take a look at the example files that you can download at the end of this article you'll see that i've put the declaration in the interface part of the unit so that it's visible outside of it.
here's the declaration of the tsingleton class:
type
tsingleton = class
public
class function newinstance: tobject; override;
procedure freeinstance; override;
class function refcount: integer;
end;i added the refcount function so that we can see that it actually works, and it's often handy to be able to read how many references to the object exist. it's not required, however, so you don't have to add it to your singleton classes if you don't need it.
ok, now for the implementation of the three methods:
procedure tsingleton.freeinstance;
begin
dec( ref_count );
if ( ref_count = 0 ) then
begin
instance := nil;
// destroy private variables here
inherited freeinstance;
end;
end;
class function tsingleton.newinstance: tobject;
begin
if ( not assigned( instance ) ) then
begin
instance := inherited newinstance;
// initialize private variables here, like this:
// tsingleton(result).variable := value;
end;
result := instance
inc( ref_count );
end;
class function tsingleton.refcount: integer;
begin
result := ref_count;
end;and that's it!
when you call tsingleton's constructor, a call is placed to the newinstance method declared in tobject. this method allocates memory to hold the new object and returns it to the constructor. the constructor uses that memory and eventually returns a pointer to the memory to the code that called the constructor. this pointer is usually stored in a variable while the object is in use.
i have overridden the newinstance method so it will allocate the memory only if no instance of the class exists. if there is an existing instance, the function simply returns that instance to the constructor so it will be reused.
if we call the constructor three times, an object is created only the first time. the other two calls simply reuse the first object. the reference count variable let us know that we have three references to the single instance.
when the program calls the destructor, a call to freeinstance is placed to free the memory allocated in the constructor. this method, too, is overridden so that the object is destroyed only when the last reference is removed.
if you intend to use a singleton in a multithreaded program, treat the object as you would any variable you share between threads. because that's just what you do: share it between the threads. so you must take special care when changing data.
simplicity itself
as you can see, creating a singleton class doesn't require much effort, just the right knowledge and a few lines of code. my code works fine in delphi 5. the technique will probably work fine with older versions of delphi, but i haven't tested it so i don't make any guarantees.