Jul 13, 2013

Prototype Design Pattern

2 comments
Prototype design pattern is a pattern where new instances of a class are created by cloning an initial object.

Reasons for using this design pattern are, - Creating an object is time consuming and a costly affair and you already have a most similar object instance in hand. - Instead of going through a time consuming process to create a complex object, just copy the existing similar object and modify it according to your needs.

UML Class Diagram
When we are not in a situation, in which we can call an object constructor directly, we will clone a pre-existing instance of the object (our prototype). Rather than creating more and more instances of said object, it is possible to save overhead by cloning an existing copy of your object. When working with this design pattern, we must create a class using the abstract modifier.i.e., It will be the only class our object will implement/inherit from. One thing to consider when using this design pattern is, determining whether we want a deep clone or shallow clone.

Shallow clone:
       It can be performed using the Object.MemberwiseClone() Method, which copies the non-static fields of the original object. With reference types the reference is copied, it means original and cloned object reference to the same instance. So, if a value is changed in the original object then shallow cloned object will be updated automatically.

Deep Clone:
         It copies reference and value types, giving two different instances of an object, It means if original object is modified the cloned object remains unchanged. While this may be preferred in some cases, remember that there is more overhead when performing a deep clone.
In the following example, we will give the option for either a deep or shallow clone of an object, but make sure you know what kind of clone you need before performing the clone.

Implementation:
For this there will be three classes in play:
     - EmployeePrototype Declares an interface for cloning itself
     - Employee The actual class we'll be prototyping

So let's construct a small sample application showing how the Prototype Design Process works. First we create EmployeePrototype Class. This class will be marked as abstract, meaning it will be the only class our Employee class can inherit from. So here is our protypical class:
using System;

namespace DesignPatterns.Prototype
{
    public abstract class EmployeePrototype
    {
        public abstract EmployeePrototype Clone(bool isDeepClone);
    }
}

Now create actual Employee class by using EmployeePrototype.
using System;

namespace DesignPatterns.Prototype
{
    public class Employee : EmployeePrototype
    {
        private string _firstName;
        private string _lastName;

        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        public Employee(string firstName, string lastName)
        {
            _firstName = firstName;
            _lastName = lastName;
        }

        public override EmployeePrototype Clone(bool isDeepClone)
        {
            if (isDeepClone)
                return new Employee(_firstName, _lastName);
            return MemberwiseClone() as Employee;
        }
    }
}

Now we have our prototype pattern set up, let's see how it works when we put it into action.
private void btnPrototype_Click(object sender, EventArgs e)
{
   Employee emp = new Employee("fName", "lName");
   Employee deepCopy = emp.Clone(true) as Employee;
}

In the above example, we created a new employee object by cloning the first employee we created. Now we can modify this new employee and it cannot affect the original copy, since we did a deep clone. If it is shallow clone, when we modified the Cloned Employee it would have also updated the original employee we created.

2 comments :