Factory Design Pattern Vs. Factory Method Design Pattern

This article gets straight to the point of the differences between the Factory Design Pattern and the Factory Method Pattern. We have all used the Factory Pattern in our projects and so the intention is not to explain that again but to clarify the basic differences between the Factory Pattern and the Factory Method Pattern that confuses most people.

This pattern is a Creational Pattern, in other words this pattern concentrates on creating instances of an object.

In simple terms here is the gist of both the patterns.

Factory Pattern

A client uses a Factory Class to get instances of classes that implement the same interface or are derived from the same base class.

FactoryPattern1.jpg

Factory Method Pattern

The client maintains a reference to the abstract creator class but instantiates with one of the sub-classes. The responsibility of creating the instance is delegated to the sub-class methods. So the name Factory Method Pattern.

FactoryPattern2.jpg

The client refers to the abstract creator class but uses one of the sub-classes to get the actual instance. In the preceding, abstract method M() creates the actual instance and returns the reference to the client.
Now let us check the program to understand both of them.

Factory Design Pattern

Class diagram for the example:

FactoryPattern3.jpg

Figure 1: Factory Pattern Class diagram

"MyFactory" is the static class that has the responsibility of creating an actual instance of the concrete class and sends the reference to the client class. "Subject" is the abstract base class from which the two classes "Physics" and "Mathematics" inherit. So, the client uses the class "MyFactory" to get the actual instances.

Here is the code:

This example uses a C# Console Application project to demonstrate the Factory Pattern.

namespace FactoryPattern

{

   /// <summary>

   /// Base class

   /// </summary>

    abstract class Subject

    {

       public abstract string SubjectName { get; }

    }

}

 

 

namespace FactoryPattern

{

    /// <summary>

    /// Class Physics inherit the class Subject and

    /// overrides the property SubjectName

    /// </summary>

    class Physics:Subject

    {

 

        public override string SubjectName

        {

            get { return "Physics"; }

        }

    }

}

 

 

namespace FactoryPattern

{

    /// <summary>

    /// Class Mathematics inherit the class Subject and

    /// overrides the property SubjectName

    /// </summary>

    class Mathematics:Subject

    {

        public override string SubjectName

        {

            get { return "Mathematics"; }

        }

    }

}

 

 

 

namespace FactoryPattern

{

    /// <summary>

    /// Factory Class that takes the responsibility

    /// of creating the instances.

    /// </summary>

    static class MyFactory

    {

        public static Subject GetName(int id)

        {

            if (id == 0)

                return new Mathematics();

            else

                return new Physics();

        }

    }

}

 

using System;

 

namespace FactoryPattern

{

    /// <summary>

    /// Program class

    /// </summary>

    class Program

    {

        static void Main(string[] args)

        {

            //Call the factory class method to get the instance

            var subject = MyFactory.GetName(0);

            //display the instance's property value

            Console.WriteLine(subject.SubjectName);

            //now invoke the other method by passing another value

            subject = MyFactory.GetName(1);

            //display the subject name property value

            Console.WriteLine(subject.SubjectName);

            Console.Read();

        }

    }

}

Here is the output:

FactoryPattern4.jpg

In this example, we saw how the Factory class takes care of creating instances and how the client program uses the Factory class to create the instances.

Note: Whenever you have a requirement to create instances of multiple types or kinds of classes then we would be using the Factory Design Pattern.

Now we will use the same example above and see how to implement it in the Factory Method Pattern.

Factory Method Pattern

Class Diagram:

FactoryPattern5.jpg

Figure 2: Factory Method Pattern

In this example, instead of a Factory class to create the instances, the sub-classes of the Factory class decides which instance of the class is to be created. The Factory class can now be called a Creator class and it has one abstract method of return type of the concrete base class. All the sub-classes of this creator class are supposed to override the abstract method and return the instance of the concrete class.

Here the class SubjectCreator is the abstract class that has an abstract method of type Subject and there are two instances of the SubjectCreator: the PhysicsCreator class that overrides the base class's abstract method and returns the type "Physics". The MathematicsCreator class overrides the base class's abstract method and returns the type "Mathematics". Now the client program, instead of creating an instance of the Creator class, now creates instances of sub-classes to get the actual instances.

Here is the code:
 

namespace FactoryMethodPattern

{

    /// <summary>

    /// Base class for concrete classes

    /// </summary>

   abstract class Subject

    {

       public abstract string SubjectName { get; }

    }

}

 

 

namespace FactoryMethodPattern

{

    /// <summary>

    /// Physics class

    /// </summary>

    class Physics:Subject

    {

 

        public override string SubjectName

        {

            get { return "Physics"; }

        }

    }

}

 

 

namespace FactoryMethodPattern

{

    /// <summary>

    /// Mathematics class

    /// </summary>

    class Mathematics:Subject

    {

        public override string SubjectName

        {

            get { return "Mathematics"; }

        }

    }

}

 

 

namespace FactoryMethodPattern

{

    /// <summary>

    /// Base class that has abstract method of type subject

    /// </summary>

    abstract class SubjectCreator

    {

        public abstract Subject FactoryMethod();

    }

}

 

namespace FactoryMethodPattern

{

    /// <summary>

    /// Class that creates instance of Physics class

    /// </summary>

    class PhysicsCreator:SubjectCreator

    {

        public override Subject FactoryMethod()

        {

            return new Physics();

        }

    }

}

 

 

namespace FactoryMethodPattern

{

    /// <summary>

    /// Class to cretae the instance of Mathematics

    /// </summary>

    class MathematicsCreator:SubjectCreator

    {

 

        public override Subject FactoryMethod()

        {

            return new Mathematics();

        }

    }

}

 

using System;

 

namespace FactoryMethodPattern

{

    class Program

    {

        static void Main(string[] args)

        {

            SubjectCreator objectCreator = new MathematicsCreator();

            Console.WriteLine(objectCreator.FactoryMethod().SubjectName);

            objectCreator = new PhysicsCreator();

            Console.WriteLine(objectCreator.FactoryMethod().SubjectName);

            Console.Read();

           

        }

    }

}


Now let us check the output:

FactoryPattern6.jpg

In this case, instead of creating instances through the Factory class, the client program delegates the responsibility of creating the instances using one of the sub-classes of the Factory class and the abstract method is overridden to return the actual instances.

Hope you like this article. Happy coding!!!

Up Next
    Ebook Download
    View all
    Learn
    View all