Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Prototype Design Pattern Introduction

Suggested Videos
Part 11 - Builder Desing Pattern Introduction - Text - Slides
Part 12 - Builder Design Pattern Implementation - Text - Slides
Part 13 - Fluent Builder Design Pattern - Text - Slides

In this video we will learn
  1. What is prototype design pattern
  2. Implementation guidelines
  3. Shallow and deep copy

Gang of Four Definition : "Prototype Design Pattern Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype"

To simplify, instead of creating object from scratch every time, you can make copies of an original instance and modify it as required.

Prototype is unique among the other creational patterns as it doesn't require a class but only an end object.


We need to choose Prototype Design Pattern when
  • Creating an object is an expensive operation and it would be more efficient to copy an object.
  • System should be independent of how its products are created, composed, and represented.
  • Objects are required that are similar to existing objects.
  • We need to hide the complexity of creating new instance from the client.
Prototype Representation
prototype pattern c#

Shallow and Deep Copy : The idea of using copy is to create a new object of the same type without knowing the exact type of the object we are invoking.

Shallow Copy : copies an object's value type fields into the target object and the object's reference types are copied as references into the target object.
prototype design pattern in .net c#

Deep Copy : Deep Copy copies an object's value and reference types into a complete new copy of the target objects.
prototype design pattern in c#

Step 1 : Create Employee class and define Shallow and Deep copy methods

Step 2 : Create Employee Address Object and override ToString() method

public class Employee
{
    public string ID { get; set; }
    public string Name { get; set; }
    public Address EmpAddress { get; set; }

    #region Copy Methods
    public Employee ShallowCopy()
    {
        return (Employee)this.MemberwiseClone();
    }

    public Employee DeepCopy()
    {
        Employee other = (Employee)this.MemberwiseClone();
        other.EmpAddress = new Address(this.EmpAddress.DoorNumber,
            this.EmpAddress.StreetNumber, this.EmpAddress.Zipcode,
            this.EmpAddress.Country);
        return other;
    }

    public override string ToString()
    {
        return string.Format("Emp ID :{0}, Emp Name : {1}, {2}",
            this.ID, this.Name, this.EmpAddress.ToString());
    }

    #endregion
}

public class Address
{
    public Address() { }
    public Address(int doorNumber, int streetNumber,
        int zipCode, string country)
    {
        this.Country = country;
        this.DoorNumber = doorNumber;
        this.StreetNumber = streetNumber;
        this.Zipcode = zipCode;
    }

    public int DoorNumber { get; set; }
    public int StreetNumber { get; set; }
    public int Zipcode { get; set; }
    public string Country { get; set; }
    public override string ToString()
    {
        return string.Format(Environment.NewLine + "Emp Address: {0}",
            string.Format("{0}, {1}, {2}, {3}",
            this.DoorNumber, this.StreetNumber,
            this.Zipcode.ToString(), this.Country));
    }
}

Step 3 : Create a windows form and define buttons and action methods for shallow and deep copy.
shallow copy vs deep copy c#

Step 4 : Add the below code and button click events for  Save, Refresh, Clear, Shallow and Deep copy  events for the windows form application.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    Employee emp = new Employee() { EmpAddress = new Address() };
    Employee empCopied;

    StringBuilder sb = new StringBuilder();

    private void btnShallow_Click(object sender, EventArgs e)
    {
        empCopied = (Employee)this.emp.ShallowCopy();
        Refresh();
    }

    private void btnDeep_Click(object sender, EventArgs e)
    {
        empCopied = (Employee)this.emp.DeepCopy();
        Refresh();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        lblResult.ResetText();
        emp.ID = txtId.Text;
        emp.Name = txtName.Text;
        emp.EmpAddress.Country = txtCountry.Text;
        emp.EmpAddress.DoorNumber = int.Parse(txtDoor.Text);
        emp.EmpAddress.StreetNumber = int.Parse(txtStreet.Text);
        emp.EmpAddress.Zipcode = int.Parse(txtZip.Text);
        MessageBox.Show("Updated");
    }

    private void btnRefresh_Click(object sender, EventArgs e)
    {
        Refresh();
    }

    private void Refresh()
    {
        sb.Clear();
        sb.AppendLine("---------------------------------");
        sb.AppendLine(string.Format("Main Employee : {0} ",
            this.emp.ToString()));
        sb.AppendLine();
        sb.AppendLine(string.Format("Copied Employee : {0} ",
            empCopied.ToString()));
        sb.AppendLine("-----------------------------------");
        sb.AppendLine(txtOutPut.Text);

        txtOutPut.Text = sb.ToString();
    }

    private void txtClear_Click(object sender, EventArgs e)
    {
        txtOutPut.ResetText();
    }
}

Step 5 : Run the application

Step 6 : Click Save and then click Shallow copy

Step 7 : Post save, click on shallow copy and then change the address details and save the object back

Step 8 : Now, click on refresh and notice that the address details getting changed for shallow copy.

Step 9 : Click on clear and then do a deep copy of the saved employee details. Notice that deep copy copied the employee details. Now, change the address and click refresh after saving the details

Step 10 : Notice that the address details of the copied object remain same

I hope, you now got familiar with the shallow and deep copy. 
  
In our next video we will discuss the significance of shallow and deep copy in Prototype design pattern

Design Patterns tutorial for beginners

2 comments:

  1. It would be nice if you included the steps for the new project and Windows Form. Venkat would have done this.

    ReplyDelete
  2. what is the meaning of lblResult ?

    ReplyDelete

It would be great if you can help share these free resources