Widely asked in Interviews (Factory Method Design Pattern)
This resources elaborates the Factory method design pattern (Creational paterns) with Introduction, Uses, Steps-by-step approach for implementation along with Step-wise Code Snippets.
Also attached Sources code and Output.
Factory Method Design Pattern
Introduction:
"This is a Creational Pattern that defines an interface to creates objects from various concrete classes and let the sub-classes decide which concrete class to instentiate at run time."
This approach is based on the concept of "inheritance" because the object generation is done by the "Factory methods" which are implemented by the derived classes.
This pattern is widely used in application framework and library development. Where the object creation is in the control of Factory methods, but when to create what object is decided by the client/consumer class.
A typical example is: "System.Linq.Expressions.Expression class contains Lambda factory method that returns the object of LambdaExpression object with parameter and executable-body."
Another good example: "Extensions of factory method are used in the Abstract Factory design pattern"
Or: "With the help of Factory Methods Service components can be plugged-in into the application framework to provide specific functionalities under the flow of main application. And in this way the framework developer needs to focus mainly on framework and partly on the concrete functionalities of pluggable-components. Hence both, framework and concrete classes can be handled independently."
Steps:
1. Create an abstract class that defines signatures of base functionalities for concrete classes (instances of these concrete classes will be generated from factory at run time)
public abstract class absTool
{
public abstract string GetTools();
}
2. Derive concrete classes and implement GetTools() method as per their specific needs.
public class Web : absTool
{
public override string GetTools()
{
return "Asp.Net, WCF, C#, Silverlight, IIS";
}
}
public class Win : absTool
{
public override string GetTools()
{
return "Win Forms, WPF, C#";
}
}
public class Services : absTool
{
public override string GetTools()
{
return "Windows Services, Msmq, Windows server";
}
}
public class Testing : absTool
{
public override string GetTools()
{
return "NUnit, MsUnit, Fiddler";
}
}
3. Create another abstract class and define signatures of "Factory Methods" that return the specific object instances of concrete classes to the consumer/client.
public abstract class absProject
{
public List
public abstract List
}
4. Derive Instance creater clases to meet specific requirements by implementing the "Factory methods" defined in above abstract class.
public class EventManagementPortal : absProject
{
public override List
{
list.Add(new Web());
list.Add(new Testing());
return list;
}
}
public class PayrollManagementSystem : absProject
{
public override List
{
list.Add(new Win());
list.Add(new Testing());
return list;
}
}
public class ServicesPortalManagement : absProject
{
public override List
{
list.Add(new Services());
list.Add(new Testing());
return list;
}
}
You are done!!
Test your Factory methods with following Client
public class TestClass
{
public static void Test()
{
Console.WriteLine("\n(FACTORY METHOD)");
absProject EMP = new EventManagementPortal();
absProject PMS = new PayrollManagementSystem();
absProject SPM = new ServicesPortalManagement();
IList
IList
IList
Console.WriteLine("\nSkill set reuired for EventManagementPortal ...\n");
int count = 1;
foreach (absTool tool in toolsForEMP)
{
Console.WriteLine(String.Format(" {0}. {1}", count++, tool.GetTools()));
}
Console.WriteLine("\nSkill set reuired for PayrollManagementSystem ...\n");
count = 1;
foreach (absTool tool in toolsForPMS)
{
Console.WriteLine(String.Format(" {0}. {1}", count++, tool.GetTools()));
}
Console.WriteLine("\nSkill set reuired for ServicesPortalManagement ...\n");
count = 1;
foreach (absTool tool in toolsForSPM)
{
Console.WriteLine(String.Format(" {0}. {1}", count++, tool.GetTools()));
}
Console.ReadKey();
}
}Source Code - Attached!
Output Snapshot - Attached!
Hope this clears the concept.
Thanks.
Updated