P:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
原形模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节。
在.NET中,抽象类Prototype是用不着的,在System命名空间中提供了ICloneable接口,其中就是唯一一个方法是Clone(),只要实现这个接口就完成了原型模式了。
以下是简历UML图:
class Resume:ICloneable
{
private string name;
private string sex;
private int age;
private string timeArea;
private string company;
public Resume(string name)
{
this.name = name;
}
public void SetPersonalInfo(string sex, int age)
{
this.sex = sex;
this.age = age;
}
public void SetWorkExperience(string timeArea, string company)
{
this.timeArea = timeArea;
this.company = company;
}
public void Display()
{
Console.WriteLine("{0} {1} {2}", name, sex, age.ToString());
Console.WriteLine("{0} {1}", timeArea, company);
}
public Object Clone()
{
return (Object)this.MemberwiseClone();
}
}
客户端代码:
Resume r1 = new Resume("龚辉");
r1.SetPersonalInfo("男", 28);
r1.SetWorkExperience("2006-08", "XX科技有限公司");
Resume r2 = (Resume)r1.Clone();
r2.SetWorkExperience("2008-09", "YY软件集团");
Resume r3 = (Resume)r1.Clone();
r3.SetPersonalInfo("女", 23);
r1.Display();
r2.Display();
r3.Display();
总结:一般在初始化的信息不发生变化的情况下,克隆是最好的办法。既隐藏了对象创建的细节,又对性能是大大的提高。
不用重新初始化对象,而是动态地获得对象运行时的状态。
MemberwiseClone()方法是这样,如果字段是值类型的,则对该字段执行逐位复制,如果字段是引用类型,则复制引用但不复制引用的对象;因此,原始对象及其复本引用同一对象。
复制引用类型(浅复制)的例子:
class WorkExperience
{
private string workDate;
private string company;
public string WorkDate
{
get { return workDate; }
set { workDate = value; }
}
public string Company
{
get { return company; }
set { company = value; }
}
}
class Resume:ICloneable
{
private string name;
private string sex;
private int age;
private WorkExperience work;
public Resume(string name)
{
this.name = name;
work = new WorkExperience();
}
public void SetPersonalInfo(string sex, int age)
{
this.sex = sex;
this.age = age;
}
public void SetWorkExperience(string workDate, string company)
{
work.WorkDate = workDate;
work.Company = company;
}
public void Display()
{
Console.WriteLine("{0} {1} {2}", name, sex, age.ToString());
Console.WriteLine("工作经历:{0} {1}", work.WorkDate, work.Company);
}
public Object Clone()
{
return (Object)this.MemberwiseClone();
}
}
Resume r1 = new Resume("龚辉");
r1.SetPersonalInfo("男", 28);
r1.SetWorkExperience("2006-08", "XX科技有限公司");
Resume r2 = (Resume)r1.Clone();
r2.SetWorkExperience("2008-09", "YY软件集团");
Resume r3 = (Resume)r1.Clone();
r3.SetWorkExperience("2010-05", "ZZ计算机公司");
r1.Display();
r2.Display();
r3.Display();
运行结果是:
r1,r2,r3三个引用都是最后一次设置,三个引用都是指向同一个对象。这个就是“浅复制”,被复制的对象的所有变量都含有与原来的对象相同的值,而所有对其它对象的引用都仍然指向原来的对象。
深复制:把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。
深复制的例子:
class WorkExperience:ICloneable
{
private string workDate;
private string company;
public string WorkDate
{
get { return workDate; }
set { workDate = value; }
}
public string Company
{
get { return company; }
set { company = value; }
}
public Object Clone()
{
return (Object)this.MemberwiseClone();
}
}
class Resume:ICloneable
{
private string name;
private string sex;
private int age;
private WorkExperience work;
public Resume(string name)
{
this.name = name;
work = new WorkExperience();
}
public Resume(WorkExperience work)
{
this.work = (WorkExperience)work.Clone();
}
public void SetPersonalInfo(string sex, int age)
{
this.sex = sex;
this.age = age;
}
public void SetWorkExperience(string workDate, string company)
{
work.WorkDate = workDate;
work.Company = company;
}
public void Display()
{
Console.WriteLine("{0} {1} {2}", name, sex, age.ToString());
Console.WriteLine("工作经历:{0} {1}", work.WorkDate, work.Company);
}
public Object Clone()
{
Resume r = new Resume(this.work);
r.name = this.name;
r.sex = this.sex;
r.age = this.age;
return r;
}
}
Resume r1 = new Resume("龚辉");
r1.SetPersonalInfo("男", 28);
r1.SetWorkExperience("2006-08", "XX科技有限公司");
Resume r2 = (Resume)r1.Clone();
r2.SetWorkExperience("2008-09", "YY软件集团");
Resume r3 = (Resume)r1.Clone();
r3.SetWorkExperience("2010-05", "ZZ计算机公司");
r1.Display();
r2.Display();
r3.Display();
运行结果: