#948 – Using Generic Lazy Class to Implement the Singleton Pattern

The singleton pattern is useful in cases when you only want/need a single instance of a type, e.g. to use in creating instances of some other type (an object factory).

We can make use of the Lazy<T> type to implement a singleton that is created as late as possible, i.e. just before it is used.

    public sealed class DogFactory
    {
        // Instance created when first referenced
        private static readonly Lazy<DogFactory> instance =
            new Lazy<DogFactory>(() => new DogFactory());

        // Prevent instantiation
        private DogFactory() { }

        public static DogFactory Instance
        {
            get { return instance.Value; }
        }

        // Actual methods go here, e.g.:
        public Dog CreateDog(string name)
        {
            return new Dog(name);
        }
    }

Notice that we can access other static members of the DogFactory without forcing the instance object to get created. The instance will only be instantiated when we use the Instance property.

Advertisement

#823 – A Nested Factory Class Implemented as a Singleton

Below is an instance of the factory pattern, with the factory class nested within the class that it creates instances for, implemented as a singleton.

    public class Dog
    {
        public string Name { get; set; }

        private Dog(string name)
        {
            Name = name;
        }

        public class Factory
        {
            // Instance created when first referenced
            private static readonly Factory instance = new Factory();

            // Prevent early instantiation due to beforefieldinit flag
            static Factory() { }

            // Prevent instantiation
            private Factory() { }

            public static Factory Instance
            {
                get { return instance; }
            }

            // Factory method
            public Dog CreateDog(string name)
            {
                return new Dog(name);
            }
        }
    }

#786 – A Lazier Singleton Pattern

Ideally, you’d want the implementation of a singleton to be as “lazy” as possible, i.e.–create the instance as late as possible, just prior to when you need it.  This helps performance, since we only create the object if/when we need it.

In our earlier implementation, the instance of our class gets created whenever any of the static members of our class are accessed.  This might be when we first access the Instance property, but might be earlier, if we had other static members in the class.

Below is a Singleton pattern that is a bit lazier.

    public sealed class DogFactory
    {
        // Prevent instantiation
        private DogFactory() { }

        public static DogFactory Instance
        {
            get { return InstanceContainer.instance; }
        }

        private class InstanceContainer
        {
            // Prevent early instantiation due to beforefieldinit flag
            static InstanceContainer() { }

            internal static readonly DogFactory instance = new DogFactory();
        }
    }

#785 – The Singleton Pattern

Singleton is a design pattern that you can use when you have a situation where you want to limit a particular class to have at most one instance.

A singleton behaves similarly to a static class in C#, but has some advantages, in that it behaves as a traditional object.  (E.g. Can implement an interface, or be passed to a method).

Here’s a common (thread-safe) pattern for implementing a singleton in C#.

    public sealed class DogFactory
    {
        // Instance created when first referenced
        private static readonly DogFactory instance = new DogFactory();

        // Prevent early instantiation due to beforefieldinit flag
        static DogFactory() { }

        // Prevent instantiation
        private DogFactory() { }

        public static DogFactory Instance
        {
            get { return instance; }
        }

        // Actual methods go here, e.g.:
        public Dog CreateDog(string name)
        {
            return new Dog(name);
        }
    }

To use the singleton, you use the Instance property:

            Dog d = DogFactory.Instance.CreateDog("Bob");