Singleton Pattern: Generics Way

Singleton Pattern:  it involves only one class which is responsible to instantiate itself, to make sure it creates not more than one instance; in the same time it provides a global point of access to that instance.

Below implementation is a step forward with aim to make it generic so as to decouple it from a particular class type.

 
public sealed class GenericSingleton<T> where T: class,  new()
    {

        private GenericSingleton() { }

        public static T GetInstance()
        {
            return SingletonCreator.genericSingleton;
        }

        internal class SingletonCreator
        {
            internal static readonly T genericSingleton = new T();

            static SingletonCreator() { }
        }
    }
(Visited 25 times, 1 visits today)