Board index » delphi » Q: Implementing the Singleton Design Pattern?
phillip.ba...@clear.net.nz (Phillip A. Baird)
![]() Delphi Developer |
Mon, 13 Sep 1999 03:00:00 GMT
|
phillip.ba...@clear.net.nz (Phillip A. Baird)
![]() Delphi Developer |
Mon, 13 Sep 1999 03:00:00 GMT
Q: Implementing the Singleton Design Pattern?I'm trying to implement the Singleton design pattern, i.e. a class The example I've seen (in C++) uses a static member function which Am I right in thinking I cannot do this in Delphi? Delphi doesn't Any ideas? thanks Phillip B. |
Ray Lischn
![]() Delphi Developer |
Mon, 13 Sep 1999 03:00:00 GMT
Re:Q: Implementing the Singleton Design Pattern?On Thu, 27 Mar 1997 10:27:59 GMT, phillip.ba...@clear.net.nz (Phillip QuoteA. Baird) wrote: 1. Use a class or global function to return an object instance. Tell 2. Override NewInstance and FreeInstance. The first time NewInstance This is probably the best approach because TObject.Create is public. Quote>Am I right in thinking I cannot do this in Delphi? Delphi doesn't units. You can store private variables in the implementation part of a unit. -- Ray Lischner (send email to "lisch" at tempest-sw.com) Author of Secrets of Delphi 2 (http://www.tempest-sw.com/secrets/) |
mark.horri..
![]() Delphi Developer |
Fri, 17 Sep 1999 03:00:00 GMT
Re:Q: Implementing the Singleton Design Pattern?In article <5hdi4l$12...@fep4.clear.net.nz> phillip.ba...@clear.net.nz (Phillip A. Baird) writes: Quote>I'm trying to implement the Singleton design pattern, i.e. a class Quote> Delphi doesn't section a pointer initilazed to nil (typed constant, or use initialization section) . Your Create function can check this pointer to see if it is nil. Mark Horridge email: mark.horri...@buseco.monash.edu.au |
Joachim Durchhol
![]() Delphi Developer |
Fri, 17 Sep 1999 03:00:00 GMT
Re:Q: Implementing the Singleton Design Pattern?But you can have global variables (that are equivalent to C++'s unit MySingleton; interface class function TheSingleton: Singleton; implementation var constructor Create; function TheSingleton: Singleton; end. Regards, |
Departamento de Informátic
![]() Delphi Developer |
Mon, 20 Sep 1999 03:00:00 GMT
Re:Q: Implementing the Singleton Design Pattern?QuotePhillip A. Baird wrote: unit StaticEx; type TObjClass = class(TObject) var Obj: TObjClass implementation var StaticCounter: longint; <--- This is a static variable. procedure ObjClass.Create(....); end; |
Phillip A. Bai
![]() Delphi Developer |
Fri, 24 Sep 1999 03:00:00 GMT
Re:Q: Implementing the Singleton Design Pattern?Thanks to those who assisted with this problem. Below is the class I It features a global StringList to keep track of instance pointers. The NewInstance method has been overwritten to raise an exception if Feel free to comment on my code or methodology. Phillip Baird unit Singleton; interface uses type TSingleton = class(TObject) ESingleton = class(Exception); implementation var destructor TSingleton.Destroy; class function TSingleton.Instance: TSingleton; // Return existing instance or create a new instance. class function TSingleton.NewInstance: TObject; end. |
1. Thoughts on the Singleton design pattern
2. Observer design pattern implemented by delegation
3. Thoughts on the Singleton pattern - To Dave Shapiro
4. Singleton pattern reveals hole in Delphi privacy
5. Singleton pattern reveals hole in Delphi privacy - maybe
6. Singleton pattern worth it or not?
7. Singleton pattern variation
8. TAutoObject and the Singleton pattern