digitalmars.D - how to implement singleton pattern for D?
- clayasaurus (5/5) Jun 17 2004 hi, i tried converting the c++ singleton from
- Mike Swieton (44/49) Jun 17 2004 Some of the specifics are different. There are odd details in C++ with w...
- clayasaurus (42/91) Jun 18 2004 when i try to compile the code
- Regan Heath (6/122) Jun 18 2004 rename the instance() method. getObj() works.
- Chr. Grade (5/148) Jun 19 2004 To express what I think about singletons and some other naive design
- Ant (4/10) Jun 18 2004 maybe a singleton is never necessary,
- Mike Swieton (15/28) Jun 20 2004 Well, the biggest problem we've run into here is that they're difficult ...
- Andy Friesen (3/8) Jun 18 2004 Here's one.
hi, i tried converting the c++ singleton from http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp to D but i'm having a lot of problems trying to convert it to D code. It is just a simple class but I'm not quite sure how to achieve the same effect in D. Any help is appreciated. Thanks.
Jun 17 2004
On Fri, 18 Jun 2004 02:23:17 +0000, clayasaurus wrote:hi, i tried converting the c++ singleton from http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp to D but i'm having a lot of problems trying to convert it to D code. It is just a simple class but I'm not quite sure how to achieve the same effect in D. Any help is appreciated. Thanks.Some of the specifics are different. There are odd details in C++ with which methods are written for you, which methods you need to use to prevent inherited class from breaking singletonnes, etc. Try something along the lines of this: class mySingleton { private: static mySingleton _instance = null; static this() { _instance = new mySingleton; } static ~this() { // this is probably reduntant delete _instance; } this() { // init... } ~this() { // deinit... } public: static mySingleton instance() { return _instance; } ... } I haven't tested it, but it ought to work, or at least be pretty close. You also could put the instantiation in mySingleton.instance(), which is the C++ idiom for singletons, but I think the idea of a singleton fits better with the static constructor. The assignment operator overload and the copy constructor doesn't apply in D, obviously. If this doesn't work or isn't helpful, why not post some code? Mike Swieton __ We are all born originals - why is it so many if us die copies? - Edward Young
Jun 17 2004
In article <pan.2004.06.18.03.57.53.836604 swieton.net>, Mike Swieton says...On Fri, 18 Jun 2004 02:23:17 +0000, clayasaurus wrote:when i try to compile the code class mySingleton { private: static mySingleton _instance = null; static this() { _instance = new mySingleton; } static ~this() { // this is probably reduntant delete _instance; } this() { // init... } ~this() { // deinit... } public: static mySingleton instance() { return _instance; } // ... } int main(char[][] args) { return 0; } i get the errors singleton.d(28): no identifier for declarator singleton.d(28): semicolon expected, not 'instance' singleton.d(28): TemplateIdentifier expected following instance singleton.d(28): ';' expected after template instance singleton.d(28): Declaration expected, not '(' singleton.d(33): unrecognized declaration and i'm not quite sure how to fix it. can you help me? thanks.hi, i tried converting the c++ singleton from http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp to D but i'm having a lot of problems trying to convert it to D code. It is just a simple class but I'm not quite sure how to achieve the same effect in D. Any help is appreciated. Thanks.Some of the specifics are different. There are odd details in C++ with which methods are written for you, which methods you need to use to prevent inherited class from breaking singletonnes, etc. Try something along the lines of this: class mySingleton { private: static mySingleton _instance = null; static this() { _instance = new mySingleton; } static ~this() { // this is probably reduntant delete _instance; } this() { // init... } ~this() { // deinit... } public: static mySingleton instance() { return _instance; } ... } I haven't tested it, but it ought to work, or at least be pretty close. You also could put the instantiation in mySingleton.instance(), which is the C++ idiom for singletons, but I think the idea of a singleton fits better with the static constructor. The assignment operator overload and the copy constructor doesn't apply in D, obviously. If this doesn't work or isn't helpful, why not post some code? Mike Swieton __ We are all born originals - why is it so many if us die copies? - Edward Young
Jun 18 2004
On Fri, 18 Jun 2004 19:22:21 +0000 (UTC), clayasaurus <clayasaurus_member pathlink.com> wrote:In article <pan.2004.06.18.03.57.53.836604 swieton.net>, Mike Swieton says...rename the instance() method. getObj() works. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/On Fri, 18 Jun 2004 02:23:17 +0000, clayasaurus wrote:when i try to compile the code class mySingleton { private: static mySingleton _instance = null; static this() { _instance = new mySingleton; } static ~this() { // this is probably reduntant delete _instance; } this() { // init... } ~this() { // deinit... } public: static mySingleton instance() { return _instance; } // ... } int main(char[][] args) { return 0; } i get the errors singleton.d(28): no identifier for declarator singleton.d(28): semicolon expected, not 'instance' singleton.d(28): TemplateIdentifier expected following instance singleton.d(28): ';' expected after template instance singleton.d(28): Declaration expected, not '(' singleton.d(33): unrecognized declaration and i'm not quite sure how to fix it. can you help me? thanks.hi, i tried converting the c++ singleton from http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp to D but i'm having a lot of problems trying to convert it to D code. It is just a simple class but I'm not quite sure how to achieve the same effect in D. Any help is appreciated. Thanks.Some of the specifics are different. There are odd details in C++ with which methods are written for you, which methods you need to use to prevent inherited class from breaking singletonnes, etc. Try something along the lines of this: class mySingleton { private: static mySingleton _instance = null; static this() { _instance = new mySingleton; } static ~this() { // this is probably reduntant delete _instance; } this() { // init... } ~this() { // deinit... } public: static mySingleton instance() { return _instance; } ... } I haven't tested it, but it ought to work, or at least be pretty close. You also could put the instantiation in mySingleton.instance(), which is the C++ idiom for singletons, but I think the idea of a singleton fits better with the static constructor. The assignment operator overload and the copy constructor doesn't apply in D, obviously. If this doesn't work or isn't helpful, why not post some code? Mike Swieton __ We are all born originals - why is it so many if us die copies? - Edward Young
Jun 18 2004
To express what I think about singletons and some other naive design patterns, Dickens needs to be quoted: "How Mr. Winkle, instead of shooting at the pigeon and killing the crow, shot at the crow and wounded the pigeon." Regan Heath wrote:On Fri, 18 Jun 2004 19:22:21 +0000 (UTC), clayasaurus <clayasaurus_member pathlink.com> wrote:In article <pan.2004.06.18.03.57.53.836604 swieton.net>, Mike Swieton says...rename the instance() method. getObj() works. ReganOn Fri, 18 Jun 2004 02:23:17 +0000, clayasaurus wrote:when i try to compile the code class mySingleton { private: static mySingleton _instance = null; static this() { _instance = new mySingleton; } static ~this() { // this is probably reduntant delete _instance; } this() { // init... } ~this() { // deinit... } public: static mySingleton instance() { return _instance; } // ... } int main(char[][] args) { return 0; } i get the errors singleton.d(28): no identifier for declarator singleton.d(28): semicolon expected, not 'instance' singleton.d(28): TemplateIdentifier expected following instance singleton.d(28): ';' expected after template instance singleton.d(28): Declaration expected, not '(' singleton.d(33): unrecognized declaration and i'm not quite sure how to fix it. can you help me? thanks.hi, i tried converting the c++ singleton from http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp to D but i'm having a lot of problems trying to convert it to D code. It is just a simple class but I'm not quite sure how to achieve the same effect in D. Any help is appreciated. Thanks.Some of the specifics are different. There are odd details in C++ with which methods are written for you, which methods you need to use to prevent inherited class from breaking singletonnes, etc. Try something along the lines of this: class mySingleton { private: static mySingleton _instance = null; static this() { _instance = new mySingleton; } static ~this() { // this is probably reduntant delete _instance; } this() { // init... } ~this() { // deinit... } public: static mySingleton instance() { return _instance; } ... } I haven't tested it, but it ought to work, or at least be pretty close. You also could put the instantiation in mySingleton.instance(), which is the C++ idiom for singletons, but I think the idea of a singleton fits better with the static constructor. The assignment operator overload and the copy constructor doesn't apply in D, obviously. If this doesn't work or isn't helpful, why not post some code? Mike Swieton __ We are all born originals - why is it so many if us die copies? - Edward Young
Jun 19 2004
On Sat, 19 Jun 2004 04:54:23 -0700, Chr. Grade wrote:To express what I think about singletons and some other naive design patterns, Dickens needs to be quoted: "How Mr. Winkle, instead of shooting at the pigeon and killing the crow, shot at the crow and wounded the pigeon."maybe a singleton is never necessary, but what are the dangers of using one? Ant
Jun 18 2004
On Fri, 18 Jun 2004 23:35:18 -0400, Ant wrote:On Sat, 19 Jun 2004 04:54:23 -0700, Chr. Grade wrote:Well, the biggest problem we've run into here is that they're difficult to test. The most significant issue is that it's more difficult to ensure that the program is in a clean state before each unit test. Of course, D has no setUp/tearDown facility in it's unit testing anyway (D's unit testing has many such oversights. Please, Walter, let us fix it!). Generally, singleton's are not a good replacement for a global variable: that's not their purpose. My general rule is that they should only be used to represent something that there really is only one of, hardware, for instance being the canonical example. Mike Swieton __ I figure that if God actually does exist, He's big enough to understand an honest difference of opinion. - Isaac AsimovTo express what I think about singletons and some other naive design patterns, Dickens needs to be quoted: "How Mr. Winkle, instead of shooting at the pigeon and killing the crow, shot at the crow and wounded the pigeon."maybe a singleton is never necessary, but what are the dangers of using one? Ant
Jun 20 2004
clayasaurus wrote:hi, i tried converting the c++ singleton from http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp to D but i'm having a lot of problems trying to convert it to D code. It is just a simple class but I'm not quite sure how to achieve the same effect in D. Any help is appreciated. Thanks.Here's one. -- andy
Jun 18 2004