class allocators should be more encapsulated

Benji Smith dlanguage at benjismith.net
Fri Dec 29 09:28:11 PST 2006


Luís Marques wrote:
> Hello all.
> 
> I need to build a class for which there should be only one instance with 
> a given attribute (of type char[]).

Isn't this the standard idiom for this kind of keyed singleton pattern?

class MyClass {

   private MyClass[char[]] instances;

   private this(char[] data) {
     // Do stuff
   }

   public static MyClass getInstance() {
     if (data in instances) {
       return instances[data];
     } else {
       MyClass obj = new MyClass(data);
       instances[data] = obj;
       return obj;
     }
   }

}

I don't see why a custom allocator ever needs to be involved.

--benji



More information about the Digitalmars-d mailing list