Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Windows Development  »  Prototype Pattern in C# and VB.NET
Prototype Pattern in C# and VB.NET
by Ashish Jaiman | Published  03/06/2002 | Windows Development | Rating:
Ashish Jaiman

Ashish works as a Sr. Software Developer with Lexign Inc., an end-to-end trusted electronic transaction management software provider (http://www.lexign.com).

Ashish has 6 years of experience in designing and developing distributed systems, server technologies and web based software applications using Microsoft technologies, Java, XML and .NET. He holds MCSD (Microsoft Certified Software Developer) and SCJP (Sun Certified Java Programmer) certifications.

Contact Ashish at jaimanalwar@yahoo.com or ashish.jaiman@lexign.com.

 

View all articles by Ashish Jaiman...
Prototype Pattern in C# and VB.NET

Article source code: prototype.zip

The Prototype pattern is used when creating an instance of a class is very time-consuming or complex in some way. Then, rather than creating more instances, you make copies of the original instance, modifying them as appropriate. Prototypes can also be used whenever you need classes that differ only in the type of processing they offer i.e. when there are many subclasses that differ only in the kind of objects they create a Prototype Pattern can be used to reduce the number of subclasses by cloning a prototype. Prototype Design Pattern helps in reducing number of classes.

In this pattern the catch is "Create objects by cloning". We sometimes create many subclasses that differ only in the kind of objects they create. In place of many subclasses creating different objects, we can have only one subclass that keeps reference to each object's base class and to create the object, clones the object passed as parameter to the constructor argument of the subclass. Each object implements the clone method so that it can be cloned. We can use Prototype pattern to reduce the number of subclasses by cloning a prototype.

Cloning can be achieved by implementing ICloneable of the System namespace. The only member of ICloneable interface is Clone method that returns a new instance of a class with the same value as an existing instance.

ICloneable.Clone method signature

[Visual Basic] Function Clone() As Object
[C#] object Clone();

We must understand that the Clone() method does a Shallow copy not Deep copy. So it just returns a reference to the original copy unlike in Deep copy where a duplicate instance is created. Any changes made to the clone will be reflected in the original copy and vice versa. We can implement Deep copy by using ISerializable interface. One other drawback is that each subclass of Prototype must implement Clone() method. For example, adding clone method may be difficult when classes under consideration already exist.

In the example I have created an EmpData class that implements ICloneable and ISerializable interfaces, ICloneable interface is required to mark the class as Cloneable and Clone method is implemented. ISerializable interface is used to implement deep copy (clone) for an EmpData Class, the trick I have used it to serialize the EmpData in a file and Deserialize the file and create another EmpData object, that would copy the Emp objects rather then copying their references as Clone method does.

The EmpData class has two methods GetEmpData and ChangeEmpData that will get the EmpData as a string and change the data of all the Emp classes. Each of the methods can be called and be used to verify the difference between shallow and deep cloning. In shallow cloning if the EmpData is changed then the changes also appear in the clone of the EmpData object, in deep copy once the EmpData object is cloned the changes would not appear in the clone if the data of the cloned object changes.

The construction of the EmpData is time consuming as it reads an XML File and creates Emp objects.

XML File

<employees>
    <employee>
        <firstname>ashish</firstname>
        <lastname>jaiman</lastname>
    </e