Sunday, May 12, 2013

Factory Design Pattern vs. Factory Method Design Pattern


This article discusses straight to the point the difference between Factory Design Pattern and Factory Method Pattern. We all have used Factory Pattern in our projects and so the intention is not to explain the same again but to clarify the basic difference between Factory Pattern and Factory Method pattern which most of the people often gets confused.
This pattern is a Creational Pattern i.e. this pattern concentrates on creating instances of an object.
In simple terms here is the gist of both the patterns:
Factory Pattern:
Client uses Factory Class to get instances of classes that are implementing the same interface or derived from same base class.

Factory Method Pattern:
Client maintains reference to the abstract creator class but instantiates with one of the sub classes.  Responsibility of creating the instance is delegated to the sub class methods. So the name Factory Method pattern.

Client refers to the abstract creator class but uses one of the sub classes to get the actual instance. In the above, abstract method M() takes care of creating the actual instance and returning the referencing to the client.
Now let us check the program to understand the both.
Factory Design Pattern:
Class diagram for the example:

Figure 1: Factory Pattern Class diagram
“MyFactory” is the static class that takes the responsibility of creating 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, client uses the class “MyFactory” to get the actual instances.
Here is the code:
This example uses the 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:

In this example, we saw how the Factory class takes care of creating instances and how client program uses the Factory class to create the instances.
Note: Whenever you have requirement to create instances of different types or kinds of classes then we would be using Factory design pattern.
Now we will take the same example above and see how we can implement it in the Factory Method pattern.
Factory Method Pattern:
Class Diagram:

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 class is to be created. The Factory class can now be called as 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 returns the instance of the concrete class.
Here class SubjectCreator is the abstract class that has an abstract method of type Subject and there are two instances of the SubjectCreator: PhysicsCreator class which overrides the base class’s abstract method and returns the type “Physics”. MathematicsCreator class overrides the base class’s abstract method and returns the type “Mathematics”. Now client program instead of creating instance of the Creator class now creates the 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:

In this case, instead of creating instances through Factory class, 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!!!