ProtoType Pattern
Given a key, the program creates an object of the required type, not by instantiation, but by copying a clean instance of the class.
The beauty of the patters is it uses the pre-existing objects to get the data and references.
Example:
public abstract class WorkForce
{
public abstract WorkForce Clone();
public string Name { get; set; }
public string Role { get; set; }
}
public class Scriptors : WorkForce
{
public string technology { get; set; }
public override WorkForce Clone()
{
return (WorkForce)MemberwiseClone();
}
public override string ToString()
{
return string.Format("Name:{0} - Role:{1} - Technology:{2}", Name, Role, technology);
}
}
public class OdinTeam : WorkForce
{
public string Language { get; set; }
public int Experience { get; set; }
public override WorkForce Clone()
{
return (WorkForce)MemberwiseClone();
}
public override string ToString()
{
return string.Format("Name:{0} - Role:{1} - Language:{2}-Experience:{3} years", Name, Role, Language, Experience);
}
}
The classes above create the basic skeleton to be used in the pattern.
Let us now call the pattern to see the real magic
protected void Page_Load(object sender, EventArgs e)
{
DevelopersTest();
ScriptorsTest();
}
public void DevelopersTest()
{
Response.Write("ODIN Team Members");
OdinTeam ot = new OdinTeam();
ot.Name = "Pradeep";
ot.Role = "Not Defined";
ot.Language = "C#, Javascript";
ot.Experience = 5;
OdinTeam ot1 = (OdinTeam)ot.Clone();
ot.Name = "Bhupender";
Response.Write(ot.ToString());
Response.Write("");
Response.Write(ot1.ToString());
Response.Write("");
}
public void ScriptorsTest()
{
Response.Write("Scriptors Team Members");
Scriptors sts = new Scriptors();
sts.Name = "Anil";
sts.Role = "Sr. Scriptor";
sts.technology = "Flash, Adobe";
Scriptors sts1 = (Scriptors)sts.Clone();
sts1.Name = "Ramesh";
sts.Role = "Team Lead";
Response.Write(sts.ToString());
Response.Write("");
Response.Write(sts1.ToString());
}
Calling it in this manner displays the result as shown in the following screen shot.
If the data is simple type (does not contain references to other objects) it can be easily cloned and passed to the new object.
However if the object itself contains references to other objects, this techniques will not clone the referenced object, hence giving undesired output.
The first technique discussed here is generally what is termed as Shallow copy. In case of referenced objects a deep copy is what is required in real.
C# facilitates this “Deep Copy” thing. All that developers need to do is use serialization (using Streams).
Although any serialization technique can be used, the preferred and most efficient is using Memory Streams.
Shallow Copy:
MemberwiseClone is a method that is available on all objects. It copies the values of all fields and any references, and returns a reference to this copy.
However, it does not copy what the references in the object point to.
That is, it performs what is known as a shallow copy. Many objects are simple, without references to other objects, and therefore shallow copies are adequate.
Deep Copy:
To preserve the complete value of the object, including all its subobjects use a deep copy. Use Serialization for this.
Objects are copied to a given destination and can be brought back again at will.
The options for serialization destinations are several, including disks(Streams) and the Internet, but the easiest one for serializing objects is memory itself(Memory Stream).
Thus a deep copy consists of serializing and deserializing in one method.
Hope this article was helpful in clearing some of the concepts about the Prototype patterns.
Till next time… Happy Coding!!
Comments
Post a Comment