This is not a factory template. A factory will always contain some design logic, at least one new . What a factory idea: the caller does not need to worry about how objects are created. This is a single user repository.
So, first of all, instead of using an array, you should have a dictionary with an indexed type.
private static Dictionary<Type, Feed> _singletons = new Dictionary<Type, Feed>();
After that, you do not need a registration method. The dictionary should be filled automatically when receiving single numbers.
Now I assume that your Feed class has a default constructor with no arguments. In this case, you can implement the factory method directly from the abstract Feed class. We are going to use some generics here because it allows you to manage inheritance:
public abstract class Feed { public static T GetInstance<T>() where T:Feed, new() { T instance = new T();
Now back to your singleton repository.
public class FeedSingletonRepository { private static readonly object _padlock = new object(); private static Dictionary<Type, Feed> _singletons = new Dictionary<Type, Feed>(); public static T GetFeed<T>() where T:Feed { lock(_padlock) { if (!_singletons.ContainsKey(typeof(T)) { _singletons[typeof(T)] = Feed.GetInstance<T>(); } return (T)_singletons[typeof(T)]; } } }
Please note that I have included streaming behavior, which is good to do when you work with single games.
Now, if you want to get a singleton for a given type inheriting from Feed (call it SpecializedFeedType ), all you have to do is:
var singleton = FeedSingletonRepository.GetFeed<SpecializedFeedType>();
or
SpecializedFeedType singleton = FeedSingletonRepository.GetFeed();
which is the same line with a slightly different syntax.
Edit2: some syntax errors have changed.