Prototype Pattern

Definition:

"A fully initialized instance to be copied or cloned."

Provides the interface for copying object without specifying their concrete classes.

Description:

Prototype pattern provides the interface(abstract product prototype interface) for copying object(product object) without specifying their concrete classes(concrete product prototype classes).

Difference between Prototype Pattern Vs Other Creational Patterns?

Prototype Pattern doesn't create new instance and it allows to copy already created object. The Other Creational Patterns allows to create new instance or allows to create complex object using step by step approach.

What are you achieving with the help of Prototype Pattern?

Able to copy already created product instance with properties and use the copied product instance with properties.

What is the impact if you don’t use Prototype Pattern ?

Complex object with properties shall be created consecutively and created complex objects consumes more memory.

Aim:

Use already created meal plan input information object for generating consecutive meal plans.

Use cases:

The system need below four inputs to generate the meal plan.
Required Input :

  1. NumberOfDays
  2. AvailableBreakfastMenu
  3. AvailableLunchMenu
  4. AvailableDinnerMenu
Once the required inputs are gathered and set as property values of a product instance, then use the same input values for generating consecutive meal plans.

Problem Statement:

Creating meal plan product instance, setting property values of meal plan product instance is expensive process. So creating the meal plan product instance with property values again for consecutive weeks is more expensive process.

Solution:

Copying already created meal plan product instance with property values is better approach than creating meal plan product instance with property values again and again for each week meal plan.

Example with Prototype Pattern:

Prototype Pattern

MealPlanInfoPrototype : abstract class defines a abstract method for copying MealPlanResult instance.

MealPlanResult : implements MealPlanInfoPrototype class, CopyGeneratedMealPlan method clones MealPlanInfoPrototype instance.

        
public abstract class MealPlanInfoPrototype
{
    public abstract MealPlanInfoPrototype CopyGeneratedMealPlan();
}

public class MealPlanResult : MealPlanInfoPrototype
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int NumberOfDays { get; set; }
    public Dictionary<DateTime, string> GeneratedBreakfastMenu { get; set; }
    public Dictionary<DateTime, string> GeneratedLunchMenu { get; set; }
    public Dictionary<DateTime, string> GeneratedDinnerMenu { get; set; }

    public override MealPlanInfoPrototype CopyGeneratedMealPlan()
    {
        return this.MemberwiseClone() as MealPlanInfoPrototype;
    }
}

class PrototypePatternClient
{
    static void Main(string[] args)
    {
        MealPlanResult week1MealPlanInfo = new MealPlanResult();
        week1MealPlanInfo.StartDate = Convert.ToDateTime("Mar-06-2017");
        week1MealPlanInfo.EndDate = Convert.ToDateTime("Mar-12-2017");

        week1MealPlanInfo.NumberOfDays = 7;

        Dictionary<DateTime, string> breakfastMenu = new Dictionary<DateTime, string>();
        breakfastMenu.Add(Convert.ToDateTime("Mar-06-2017"), "Scrumbled Egg");
        week1MealPlanInfo.GeneratedBreakfastMenu = breakfastMenu;

        Dictionary<DateTime, string> lunchMenu = new Dictionary<DateTime, string>();
        lunchMenu.Add(Convert.ToDateTime("Mar-06-2017"), "Fried Chicken");
        week1MealPlanInfo.GeneratedLunchMenu = lunchMenu;

        Dictionary<DateTime, string> dinnerMenu = new Dictionary<DateTime, string>();
        dinnerMenu.Add(Convert.ToDateTime("Mar-06-2017"), "Grilled Atlantic Salmon");
        week1MealPlanInfo.GeneratedDinnerMenu = dinnerMenu;

        Console.WriteLine("Week-1 MealPlanInfo property values are \n StartDate : {0} \n EndDate : {1} \n NumberOfDays : {2} \n 
        GeneratedBreakfastMenu Count : {3} \n GeneratedLunchMenu Count : {4} \n GeneratedDinnerMenu Count :  {5} \n\n\n",
        week1MealPlanInfo.StartDate.ToShortDateString(), week1MealPlanInfo.EndDate.ToShortDateString(), week1MealPlanInfo.NumberOfDays,
        week1MealPlanInfo.GeneratedBreakfastMenu.Count, week1MealPlanInfo.GeneratedLunchMenu.Count, week1MealPlanInfo.GeneratedDinnerMenu.Count);

        MealPlanResult week2MealPlanInfo = (MealPlanResult)week1MealPlanInfo.CopyGeneratedMealPlan();
        week2MealPlanInfo.StartDate = Convert.ToDateTime("Mar-13-2017");
        week2MealPlanInfo.EndDate = Convert.ToDateTime("Mar-19-2017");

        Console.WriteLine("Week-2 MealPlanInfo property values are \n StartDate : {0} \n EndDate : {1} \n NumberOfDays : {2} \n 
        GeneratedBreakfastMenu Count : {3} \n GeneratedLunchMenu Count : {4} \n GeneratedDinnerMenu Count :  {5} ", 
        week2MealPlanInfo.StartDate.ToShortDateString(), week2MealPlanInfo.EndDate.ToShortDateString(), week2MealPlanInfo.NumberOfDays, 
        week2MealPlanInfo.GeneratedBreakfastMenu.Count, week2MealPlanInfo.GeneratedLunchMenu.Count, week2MealPlanInfo.GeneratedDinnerMenu.Count);

        Console.ReadLine();
    }
}