The Prototype pattern specifies object creation by copying existing instances rather than instantiating classes directly. This approach anables dynamic object generation based on predefined templates.
Appropriate Use Cases:
- When a system needs to decouple object creation from implementation details
- When class types are determined dynamically at runtime
- To avoid parlalel factory hierarchies matching product classes
- When objects exist in limited state combinations
Class Diagram Implementation:
public override Prototype Clone()
{
// Creates shallow copy by duplicating non-static fields
// Value types get bit-by-bit copies, reference types retain original references
return (Prototype)this.MemberwiseClone();
}
}
</div><div>```
/// <summary>
/// Client usage demonstration
/// </summary>
static void TestPrototypeImplementation()
{
SamplePrototype original = new SamplePrototype("A");
SamplePrototype copy = (SamplePrototype)original.Clone();
Console.WriteLine("Cloned ID: {0}", copy.Id);
Console.Read();
}
Shalllow Copy Example:
public ResumeTemplate(string name)
{
this.name = name;
}
public void SetBasicInfo(string gender, string age)
{
this.gender = gender;
this.age = age;
}
public void SetExperience(string period, string company)
{
this.workPeriod = period;
this.organization = company;
}
public void DisplayProfile()
{
Console.WriteLine("{0} {1} {2}", name, age, gender);
Console.WriteLine("Experience: {0} {1}", workPeriod, organization);
}
public object Clone()
{
return this.MemberwiseClone();
}
}
static void TestResumePrototype() { ResumeTemplate profile = new ResumeTemplate("John"); profile.SetBasicInfo("Male", "30"); profile.SetExperience("2015-2018", "Tech Corp");
ResumeTemplate copy1 = (ResumeTemplate)profile.Clone();
copy1.SetExperience("2018-2021", "Media Inc");
ResumeTemplate copy2 = (ResumeTemplate)profile.Clone();
copy2.SetBasicInfo("Female", "32");
profile.DisplayProfile();
copy1.DisplayProfile();
copy2.DisplayProfile();
Console.Read();
}
</div>**Deep Copy Implementation:**
<div>```
class WorkHistory : ICloneable
{
private string employmentDate;
public string EmploymentDate
{
get { return employmentDate; }
set { employmentDate = value; }
}
private string employer;
public string Employer
{
get { return employer; }
set { employer = value; }
}
public object Clone()
{
return this.MemberwiseClone();
}
}
class EnhancedResume : ICloneable
{
private string name;
private string gender;
private string age;
private WorkHistory experience;
public EnhancedResume(string name)
{
this.name = name;
this.experience = new WorkHistory();
}
private EnhancedResume(WorkHistory history)
{
this.experience = (WorkHistory)history.Clone();
}
public void SetBasicInfo(string gender, string age)
{
this.gender = gender;
this.age = age;
}
public void SetExperience(string date, string company)
{
experience.EmploymentDate = date;
experience.Employer = company;
}
public void DisplayProfile()
{
Console.WriteLine("{0} {1} {2}", name, age, gender);
Console.WriteLine("Experience: {0} {1}", experience.EmploymentDate, experience.Employer);
}
public object Clone()
{
EnhancedResume result = new EnhancedResume(this.experience);
result.name = this.name;
result.gender = this.gender;
result.age = this.age;
return result;
}
}