0

Lets say we have a namespace called AllFoos.

And Lets say all classes in the AllFoos namespace implement a specific interface called IFoo and are all singletons.

Now we have:

HashSet<IFoo> myFoos = new HashSet<IFoo>();

What would be the code to populate the collection MyFoos with the singleton instances of all the classes in AllFoos?

The singleton implementation for all of these classes is:

private static IFoo _instance = new ConcreteImplementationOfFoo1();

public static IFoo Instance
{
     get
     {
          return _instance;
     }
}
Barka
  • 8,764
  • 15
  • 64
  • 91
  • 3
    Why in the name of $deity would you want to do that? – Oded Jun 03 '11 at 20:23
  • Isn't a collection of singletons kind of a contradiction? A Singleton, by definition, is a single object, yet you want a collection of them. – Robert Harvey Jun 03 '11 at 20:23
  • 2
    I took from the question that there are multiple classes, each of them singleton. –  Jun 03 '11 at 20:27
  • Actually, Robert, a singleton means that there is only one INSTANCE of a particular object. I believe in this case the OP is looking for several singleton objects, each which can have at most one instance – danielpops Jun 03 '11 at 20:28
  • 1
    I guess you could use reflection to get all the classes inside the namespace and create them one by one – MBen Jun 03 '11 at 20:30
  • Yes each class in the namespace implements the same IFoo interface and is a singleton. I want to create a collection of the singleton objects. These classes all have the same contract (implement a common interface), but have different behaviors in the actual implementations of the interface methods. – Barka Jun 03 '11 at 20:50
  • The descripon "Singleton" is insufficient. Do they all share a common interface or at least a design pattern? – H H Jun 03 '11 at 21:10
  • @Henk, he says they all have a common interface `IFoo` in the question. –  Jun 03 '11 at 21:25
  • But is IFoo related to the Singleton trait? – H H Jun 03 '11 at 21:26
  • and they all share the same singleton implementation: – Barka Jun 03 '11 at 21:28
  • @Henk, I was not able to implement the singleton traight in the interface, as the singleton has to return the concrete object and the within the interface there is no way to identify what that is. So the singleton is implemented in each implemented class, but they all have the same form as i have shown by adding the singleton code to the bottom of the original post above. - thanks! – Barka Jun 03 '11 at 21:57

1 Answers1

4

If you would use a dependency injection framework you could:

  1. register your classes as "singleton" in the container
  2. register all implementations easily (good frameworks allow mass-registration based on some patterns)
  3. resolve all implementations of your interface as a list

If you want to go the classic way, you have to tell how your singleton pattern looks like (e.g. static Instance property?), and it can be solved with classic reflection as mentioned in the comments already.

Tz_
  • 2,949
  • 18
  • 13