#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);
            }
        }
    }

#822 – Embed a Factory Class Inside Its Related Class

You can use the factory pattern when you want to centralize object creation within a class different from the one representing the object being created.

One problem with making the factory class independent is that the constructor in the main class still needs to be public, which allows the factory to create an object.  Other code can then circumvent the factory, creating the object directly.

One solution is to make the factory class a nested type within the main class and then make the constructor of the main class private.

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

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

        public class Factory
        {
            // Factory pattern - method for creating a dog
            public static Dog CreateDog(string name)
            {
                return new Dog(name);
            }
        }
    }

To create an instance of the object:

            // Create a dog
            Dog myDog = Dog.Factory.CreateDog("Kirby");

#821 – The Factory Pattern

Factory is a design pattern that you can use when you don’t want client code creating objects directly, but you want to centralize object creation within another class.  To get a new instance of an object, you call a method in the factory, which creates the object on your behalf.

In the example below, DogFactory.CreateDog is a factory method, which does the actual creation of a Dog object.

    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; }
        }

        // Factory pattern - method for creating a dog
        public Dog CreateDog(string name, int age)
        {
            return new Dog(name, age);
        }
    }

To create a Dog:

            Dog myDog = DogFactory.Instance.CreateDog("Kirby", 15);