Definition:
"Factory Method Pattern facilitates to create an instance of several derived classes."
Define an interface for creating object. But let subclasses decide which class to instantiate. The Factory Method lets a class defer instantiation it uses to subclasses.
Factory pattern provides the responsibility of object instantiation to a specific class(Factory class).There is no difference in Factory Method pattern and Factory pattern by definition. The intent of Factory Method pattern and Factory pattern remains same.
Description:
The Factory Method pattern is next version of Factory pattern. This pattern defines an interface(factory interface) for creating object(factory object) and provide an option to create a type of object(product) to its subclasses(concrete factory). The newly created object(product) is referred in client application using defined interface(product interface).
Difference between Factory Pattern Vs Factory Method Pattern?
Factory method pattern introduce new interface(factory interface), which is used in client application for declaring variable, which holds the instance of newly created object.
Instead Factory Pattern, concrete factory is used on client application for declaring variable, holds the instance of newly created object.
Factory Pattern | Factory Method Pattern |
---|---|
Concreate subclass of the factory interface manages the object creation process. | Factory interface is used in client application for declaring object creation. |
This is not having factory interface and Factory class manages the object creation process. | Concrete Factory class is used in client application for declaring object creation. |
Why new interface (Factory Interface) is introduced in Factory Method Pattern ?
"Program to Interface". If we add one or more classes into class hierarchy, then implement the factory interface and new class is ready for consumption from the client application.
Factory Method Pattern Program:
Abstract Product(MealPlan) : abstract product class defines the signature for generating meal plan.
Concreate Products(WeeklyPlan & MonthlyPlan) : implements abstract product class and generates the meal plan.
Abstract Factory(AbstractMealPlanFactory) : abstract factory defines the signature for creating MealPlanInstance.
Factory(MealPlanFactory) : This class handles the responsibility of object(product) instantiation process.
Client(FactoryPattern) : client application class, interacts with MealPlanFactory class using AbstractMealPlanFactory, for getting the instance of WeeklyPlan and MonthlyPlan objects and generates the meal plan by invoking GenerateMealPlan method.
public abstract class MealPlan { public abstract ListGenerateMealPlan(DateTime startDate, DateTime endDate); } public class WeeklyPlan : MealPlan { public override List GenerateMealPlan(DateTime startDate, DateTime endDate) { Console.WriteLine("Generated weekly meal plan"); return new List (); } } public class MonthlyPlan : MealPlan { public override List GenerateMealPlan(DateTime startDate, DateTime endDate) { Console.WriteLine("Generated monthly meal plan"); return new List (); } } public abstract class AbstractMealPlanFactory { public abstract MealPlan GetMealPlanInstance(MealPlanType planType); } public class MealPlanFactory : AbstractMealPlanFactory { public override MealPlan GetMealPlanInstance(MealPlanType planType) { switch (planType) { case MealPlanType.Weekly: return new WeeklyPlan(); case MealPlanType.Monthly: return new MonthlyPlan(); default: throw new NotSupportedException(); } } } class FactoryMethodClient { static void Main(string[] args) { AbstractMealPlanFactory planFactory = new MealPlanFactory(); MealPlan mealPlan = planFactory.GetMealPlanInstance(MealPlanType.Monthly); mealPlan.GenerateMealPlan(Convert.ToDateTime("11-01-2016"), Convert.ToDateTime("11-30-2016")); Console.ReadKey(); } }