Singleton pattern in D

BLS nanali at nospam-wanadoo.fr
Fri May 11 08:59:36 PDT 2007


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



More information about the Digitalmars-d-learn mailing list