Tuesday, August 18, 2009

Prototype Design Pattern in C#



Intent: The intent of Prototype pattern is to specify the kind of objects to create using a prototypical instance and create new objects by copying this prototype.

Non-Software Example:
The mitotic cell division of a cell results in two cells of identical genotype. This cell cloning is an example of prototype pattern. The cell itself takes an active role of creating a new instance itself.

The cell here corresponds to the Prototype, as it has an interface to clone itself.
The specific instance represents the concrete prototype.
The DNA or the genetic blue print corresponds to the client as it instructs the cell to divide or clone itself.



Benefits:

Prototype pattern is used when creating an instance of a class, which is complex and time consuming. Instead of creating a new instance of the object, it is possible to make copy of the original object in some way.

Software Example & Explanation of Code:

Let us assume that we are building an ASP.Net page where we have a requirement to update the employee address. As a part of this requirement we have to update the existing record at the same time we have to keep the track of the existing record also.

With C# prototype pattern can be very easily implemented by using the “ICloneable” interface. Any class that wants to support cloning should implement “ICloneable” interface. So here in our code we have an “Employee” class which implements the “ICloneable” interface. The clone method copies the existing data and returns a new cloned object. The implementation contains the “Employee” class which implements the ICloneable interface. The default.aspx.cs page implements the prototype pattern.

Employee.cs

using System;

namespace PrototypePattern
{
public class Employee:ICloneable
{
private string _FirstName;

public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
private string _LastName;

public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
private int _EmployeeId;

public int EmployeeId
{
get { return _EmployeeId; }
set { _EmployeeId = value; }
}
private string _Address1;

public string Address1
{
get { return _Address1; }
set { _Address1 = value; }
}
private string _Address2;

public string Address2
{
get { return _Address2; }
set { _Address2 = value; }
}

public Employee()
{

}



#region ICloneable Members

public object Clone()
{
Employee objEmployee = new Employee();
objEmployee.FirstName = _FirstName;
objEmployee.LastName = _LastName;
objEmployee.Address1 = _Address1;
objEmployee.Address2 = _Address2;
objEmployee.EmployeeId = _EmployeeId;

return objEmployee;
}

#endregion
}
}

Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace PrototypePattern
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Employee objEmployee = new Employee();
objEmployee.FirstName = "FirstName-ABC";
objEmployee.LastName = "LastName-ABC";
objEmployee.EmployeeId = 1001;
objEmployee.Address1 = "Address1- Old Address1";
objEmployee.Address2 = "Address2 - Old Address2";

//Clone the object , Implimentation of Prototype Pattern
Employee objEmp = (Employee)objEmployee.Clone();

//In real time implementation the database table containing History record
//Should take the data from objEmployee object and current data should use
//the objEmp object to pass the data

objEmp.Address1 = "Address- New Address1";
objEmp.Address2 = "Address- New Address2";

Response.Write("=================================================--");
Response.Write("------------Employee Address Details-------------"+"--");
Response.Write("=================================================--");

Response.Write("Employee ID-->" + objEmployee.EmployeeId.ToString() + "----");
Response.Write("First Name-->" + objEmployee.FirstName + "----");
Response.Write("Last Name-->" + objEmployee.LastName + "----");

//This should go to Main Table
Response.Write("Latest Address1-->" + objEmp.Address1 + "----");
Response.Write("Latest Address2-->" + objEmp.Address2 + "----");

//This should go to History Table
Response.Write("Old Address1-->" + objEmployee.Address1 + "----");
Response.Write("Old Address2-->" + objEmployee.Address2 + "----");

}
}
}

The entire code is available at http://www.csharppatterns.com/

No comments: