The singleton design pattern in D, C++ and Java

Nick Sabalausky a at a.a
Fri Jul 16 11:17:26 PDT 2010


"BLS" <windevguy at hotmail.de> wrote in message 
news:i1pnfn$166f$1 at digitalmars.com...
> On 16/07/2010 15:24, Justin Johansson wrote:
>> Which language out of C++, D and Java does this classical
>> "GoF" (gang-of-four***) design pattern best?
>>
>> *** http://en.wikipedia.org/wiki/Design_Patterns
>>
>> For my take, I prefer synchronization-free implementations.
>>
>> Surely this topic has been around before???
>>
>> <bystander awareness="clueless" name="Justin"/>
>
> Note sure about "Best" but I like this D implementation.
>
> auto st1 = Singleton();
> auto st2 = Singleton();
>
> final class Singleton {
> private static Singleton st;
>
> static this() {
> st = new Singleton;
> }
>
> static Singleton opCall() {
> return st;
> }
> //....
> }

Whenever possible, I like to just make a static class. The limitation is 
that you can't pass it around, at least not in D, and IIRC there may be some 
problems having it participate in inheritance, but a lot of times that 
doesn't matter.




More information about the Digitalmars-d mailing list