digitalmars.D - Singleton
- turpeine lut.fi (10/10) Apr 08 2005 It would be great, if there could be singleton class inside language. Or...
- Georg Wrede (12/14) Apr 08 2005 Would you care to elaborate on the merits of having an in-built
- Benji Smith (19/21) Apr 11 2005 Here's the way I would do it, using current syntax:
It would be great, if there could be singleton class inside language. Or is there? singleton SFoo { this(); } .. SFoo a(); SFoo b(); if (&a==&b) // this would be true
Apr 08 2005
turpeine lut.fi wrote:It would be great, if there could be singleton class inside language. Or is there?Would you care to elaborate on the merits of having an in-built singleton class? Also, (in general,) for such things to become a part of the language, there would have to be reasons like: - hard to work around - brittleness of work around - major usefulness of new syntax - substantial increase of stability/clearness of written code or other weighty issues. There may be some, but you'd have to explain them. (At least to thickies like me. :-) )
Apr 08 2005
turpeine lut.fi wrote:It would be great, if there could be singleton class inside language. Or is there?Here's the way I would do it, using current syntax: class MySingleton { public static Object myObject = null; static { // perform some sort of initialization on myObject } // Private ctor prevents object from direct instantiation private MySingleton() {} public static getSingletor() { return myObject; } } What's wrong with that? A singleton is a simple pattern (like a factory) that can be **easily** implemented, without changing the language at all. --BenjiSmith
Apr 11 2005