www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - how to implement singleton pattern for D?

reply clayasaurus <clayasaurus_member pathlink.com> writes:
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
next sibling parent reply Mike Swieton <mike swieton.net> writes:
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
parent reply clayasaurus <clayasaurus_member pathlink.com> writes:
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:

 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
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.
Jun 18 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
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...
 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
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.
rename the instance() method. getObj() works. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 18 2004
parent reply "Chr. Grade" <tickle everymail.net> writes:
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...

 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
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.
rename the instance() method. getObj() works. Regan
Jun 19 2004
parent reply Ant <duitoolkit yahoo.ca> writes:
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
parent Mike Swieton <mike swieton.net> writes:
On Fri, 18 Jun 2004 23:35:18 -0400, Ant wrote:

 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
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 Asimov
Jun 20 2004
prev sibling parent Andy Friesen <andy ikagames.com> writes:
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