Singleton pattern in D

Mike Parker aldacron71 at yahoo.com
Fri May 11 12:16:17 PDT 2007


BLS wrote:
> I would like to implement the  GoF patterns in D, I have to start somewhere and  found the Singleton pattern interesting ( to say the least )
> 
> Here a simple *C Sharp* implementation :
> 
> class Singleton
>   {
>     private static Singleton instance;
> 
>     // Note: Constructor is 'protected'  ...  why not private ?
>     protected Singleton() 
>     {
>     }
> 
>     public static Singleton Instance()
>     {
>       // Use 'Lazy initialization' 
>       if (instance == null)
>       {
>         instance = new Singleton();
>       }
> 
>       return instance;
>     }
>   }
> 
> My thoughts
> D classes do not have nessesarily an constructor.
> OR How would you implement this pattern D
> 
> In general :
> Patterns allways have a bad side-effect, you can not really re-use them. 
> What about using D MIXINS/ MIXIN Templates and Interfaces to make pattern more re-useable, more universal . Opinions ?
> 
> regarding the singleton :
> (probabely a quit stupid question)
> What about static this()... can this D feature be usefull
> 
> Many Thanks in advance Bjoern
> 

http://dblog.aldacron.net/2007/03/03/singletons-in-d/


More information about the Digitalmars-d-learn mailing list