A
Andrew Ducker
I'm implementing a singleton using the example at:
http://www.yoda.arachsys.com/csharp/singleton.html
as a basis (second example).
However - I have about 20 classes I wish to make singletons - and I
don't want to duplicate the code in each one. The difficulty comes in
the method that returns the singleton instance. In the example it's:
if (instance == null)
instance = new Class1();
return instance;
But obviously I don't want it to always return Class1 - if I'm calling
Class2 then I want Class2 returned.
Now, I could just put this code into each class, but it seems wasteful
- is there a more efficient way of doing this?
Thanks,
Andy D
http://www.yoda.arachsys.com/csharp/singleton.html
as a basis (second example).
However - I have about 20 classes I wish to make singletons - and I
don't want to duplicate the code in each one. The difficulty comes in
the method that returns the singleton instance. In the example it's:
if (instance == null)
instance = new Class1();
return instance;
But obviously I don't want it to always return Class1 - if I'm calling
Class2 then I want Class2 returned.
Now, I could just put this code into each class, but it seems wasteful
- is there a more efficient way of doing this?
Thanks,
Andy D