Although it's hard to say exactly what you mean, one template that you can reference is the Multiton pattern , where you control the map of named instances as key-value pairs.
This is basically a factory, but each instance is created only once:
I modified the Wikipedia example a bit to show that you can even get one class if your specific implementations are private and within the original class:
class FooMultiton { private static readonly Dictionary<object, FooMultiton> _instances = new Dictionary<object, FooMultiton>();
In addition, as a rule, if singleton is not a static class, it can implement any required interface. The only thing that prevents a singleton pattern is to create multiple instances of a singleton class, but that does not mean that you cannot completely replace the implementation with something else.
For example, if you have a singleton, which is not a static class:
interface ICanTalk { string Talk(); } class Singleton : ICanTalk { private Singleton() { } private static readonly Singleton _instance = new Singleton(); public static Singleton Instance { get { return _instance; } } public string Talk() { return "this is a singleton"; } }
You can also have several different implementations:
class OtherInstance : ICanTalk { public string Talk() { return "this is something else"; } }
Then you can choose any implementation you want, but get only one instance of the Singleton class:
ICanTalk item; item = Singleton.Instance; item = new OtherInstance(); item = new YetAnotherInstance();
Groo
source share