Creating a real singleton class in Delphi 5[1]

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

本文简介:选择自 musicwind 的 blog

creating a real singleton class in delphi 5


abstract:the article describes how to create a class that follows the singleton pattern. the class described will take care of the singleton requirements and effects itself, effectively leaving the programmer to use the class as any others.

creating a real singleton class in delphi

by lasse v錱s鎡her karlsen, systems developer for cintra software engineering

errata 1. implementation of tsingleton.newinstance had the first test switched, the one that checked if the instance variable was assigned or not. -- fixed 11-08-2000 - thanks to the people that emailed me about this --
2. overzealous fix for the first errata, the newinstance method should now work as stated. -- fixed 11.08.2000 --

a singleton is a class that supports the creation of just one object. it's like your computer -- there's just one keyboard. so if you were writing delphi code that simulated your computer, you would want just one object instance to deal with keyboard read, write, and control activities.

articles about singleton classes are rare but there are a few on the internet. i've seen some in magazines too. but none of these articles include sample code to create a real singleton class.

by "real" i mean that the class itself enforces the one-instance requirement, instead of leaving that task to the programmer. all of the articles i have seen so far have required the programmer to use the class in a special way to enforce the singleton pattern.

in this article you will see how to create a proper singleton class that includes logic to enforce the one-instance rule.

note: the conventional approach, in which the one-instance rule is maintained explicitly by the programmer, is not without merit. real singleton classes like the one i present here effectively hide the details and awareness of the singleton pattern from the programmer. the programmer is relieved of the task of enforcing the pattern -- that's good -- but the programmer may also be unaware of the special nature of the class, which is bad. if you don't know that the class is a singleton class then all sorts of errors can crop up. you have been warned!

writing the code

our goal is to write a class that can be used like this:

procedure test;
var
  s1, s2 : tsingleton;
begin
  s1 := tsingleton.create;
  s2 := tsingleton.create;
  // do something with s1 and s2 here
  s2.free;
  s1.free;
end;
(i've left out the try...finally blocks and other safeguards for simplicity's sake.)

the goal is to make the tsingleton class behave in such a way that both s1 and s2 refer to the same object. here's what we have to do:

  • instantiate the object the first time create is called (when s1 is created above)
  • ensure that when another create is executed (s2 above), the existing object is reused instead of another one created
  • avoid destroying the object when it's not the last reference that is destroyed (when s2 is freed)
  • destroy the instance when the last reference is destroyed (when s1 is freed above)
is there a way to override the creation and destruction of a new object in delphi? there sure is. in the tobject class (the mother of all objects {pun intended}), there are two methods we can use:
class function newinstance: tobject; virtual;
procedure freeinstance; virtual;

newinstance is responsible for allocating memory to hold a new instance of the class, and freeinstance is responsible for freeing that memory when the class is through with it.

these methods control what happens when the object is created and when the object is destroyed. if we overwrite this code, we can alter the default behavior to work the say a singleton class requires. nothing to it.

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

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

go top