Simple Factory Design Pattern doesn’t belong to the Gangs of Four. A Simple Factory Pattern is one of that returns an instance of one of several possible classes, depending on the data provided to it. This implies that the classes it returns have same parent class and methods, but each of them perform task differently for different kind of data.
UML Class Diagram for Simple Factory Design Pattern
Now you can see in above figure that there are two sub classes ChildClass1 and ChildClass2 have same parent ParentClass, and there is a separate class Factory. Factory class a method createClass with return type ParentClass. This implies that factory can return any of the child classes instance. The child class method’s can have different implementation depends on the requirement. The method in Factory class has the logic to decide that which class needs to return. It could be very complex logic or may be very simple.
A real world example for Simple Factory Design Pattern:
Let's consider a simple C# case where we could use a Factory class. Consider a personal checkbook management program like Quicken. It manages several bank accounts and investments and can handle your bill paying. Where could you use a Factory pattern in designing a program like that?
Sample Code C# with explanation to implement Simple Factory Design Pattern for given problem.
We can start thinking that what could be our parent class. In above scenario we can consider CheckBook as parent class and let assume we have three type of check for this checkbook Travel, Health and Personal. These will be our child classes which have method to decide corresponding expenses or could be done with the help of Dependency Injection. We also need to create a CheckFactory class to implement Simple Factory Design Pattern. The UML diagram for this is shown in figure below.
Here is the class the Parent Class with a protected member and a method.
public class CheckBook
{
protected decimal _Amount;
public decimal getExpense()
{
return _Amount;
}
}
Now let’s create the other child classes. These child classes is derived from CheckBook base class.
public class Health:CheckBook
{
public Health()
{
_Amount = 5000;//Could be any logic to calculate health amount
Console.WriteLine("Your Health Expense: {0}", _Amount.ToString());
}
}
public class Travel : CheckBook
{
public Travel()
{
_Amount = 10000;//Could be any logic to calculate travel amount
Console.WriteLine("Your Travel Expense: {0}", _Amount.ToString());
}
}
public class Personal : CheckBook
{
public Personal()
{
_Amount = 15000;//Could be any logic to calculate personal amount
Console.WriteLine("Your Personal Expense: {0}", _Amount.ToString());
}
}
Building the Simple Factory
Now our Simple Factory class is easy to write. We just test for the input case provided and then return an instance of one class or the other.
public class CheckFactory
{
public CheckFactory() { }
public CheckBook chooseExpense(int o)
{
switch (o)
{
case 1:
return new Personal();
case 2:
return new Travel();
default:
return new Health();
}
}
}
Using the Factory
Let’s see how we put this together. In response to the user input, we use console application for that and ask to user to type an option and CheckFactory to return the correct derived class.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type 1 for Personal expense:");
Console.WriteLine("Type 2 for Travel expense:");
Console.WriteLine("Type any number for Health expense:");
string input = Console.ReadLine();
CheckFactory objCheckFactory = new CheckFactory();
CheckBook currentExpense = objCheckFactory.chooseExpense(int.Parse(input));
Console.WriteLine("From main class "+currentExpense.getExpense());
Console.Read();
}
}
That’s the fundamental principle of the Simple Factory design pattern. You create an abstraction that decides which of several possible classes to return, and it returns one. Then you call the methods of that class instance without ever knowing which subclass you are actually using. This approach keeps the issues of data dependence separated from the classes’ useful methods.
Summary:
In this article we have learnt the fundamental of Simple Factory design pattern. I have explained the pattern basics and than applied on a real world problem.
I hope you will find this article useful. If you have any question or suggestion regarding this article, than please make comment, i would like to reply for that.