Creating a real singleton class in Delphi 5[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 musicwind 的 blog

tracking instances is a little trickier. we must:

  1. keep track of each existing instance of our class
  2. keep track of how many references there are to this instance
  3. create a new object only when no instance exists
  4. destroy the object when the last reference is removed
to keep track of an existing instance, we will use a global variable. actually, the variable will ultimately be declared inside the implementation part of a unit so it won't be a true global variable. but the scope must be sufficient to track all the create and free calls. we'll call the variable 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.

本文关键:Creating a real singleton class in Delphi 5
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top