![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public class ConcretePrototype1 : Prototype
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// Constructor
public ConcretePrototype1(string id)
: base(id)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override Prototype Clone()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Shallow copy
return (Prototype)this.MemberwiseClone();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class ConcretePrototype2 : Prototype
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// Constructor
public ConcretePrototype2(string id)
: base(id)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override Prototype Clone()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Shallow copy
return (Prototype)this.MemberwiseClone();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
客户端:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
ConcretePrototype1 p1 = new ConcretePrototype1("I");
ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
Console.WriteLine("Cloned: {0}", c1.Id);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ConcretePrototype2 p2 = new ConcretePrototype2("II");
ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
Console.WriteLine("Cloned: {0}", c2.Id);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2. 实例
对于.NET而言,原型模式抽象类Prototype是用不着的,在.NET中System命名空间中提供了ICloneable接口,其中就是唯一的一个方法Clone(),这样我们只需要实现这个接口就可以完成原型模式了。
下面看大话设计模式中的简历的原型实现:
代码结构图:
简历类:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public class Resume : ICloneable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string name;
private string sex;
private string age;
private string timeArea;
private string company;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Resume(string name)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.name = name;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置个人信息
public void SetPersonalInfo(string sex, string age)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.sex = sex;
this.age = age;
}
//设置工作经历
public void SetWorkExperience(string timeArea, string company)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.timeArea = timeArea;
this.company = company;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//显示
public void Display()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("{0} {1} {2}", name, sex, age);
Console.WriteLine("工作经历:{0} {1}", timeArea, company);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Object Clone()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (Object)this.MemberwiseClone();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
客户端调用:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
Resume a = new Resume("大鸟");
a.SetPersonalInfo("男", "29");
a.SetWorkExperience("1998-2000", "XX公司");
Resume b = (Resume)a.Clone();
b.SetWorkExperience("1998-2006", "YY企业");
Resume c = (Resume)a.Clone();
c.SetPersonalInfo("男", "24");
a.Display();
b.Display();
c.Display();
Console.Read();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
结果显示:
大鸟 男 29
工作经历 1998-2000 XX公司
大鸟 29
工作经历 1998-2006 YY公司
大鸟 男 24
工作经历 1998-2000 XX公司
一般在初始化的信息不发生变化的情况下,克隆是最好的方法。这既隐藏了对象的创建细节,又对性能是大大的提高。
下面我们来看深克隆和浅克隆:
在上面的简历类中,数据都是string型的,而string是一种拥有值类型特点的特殊引用类型,MemberwiseClone()方法对于值类型的字段执行逐位复制,对于引用类型,则只复制引用的对象,因此,原对象及其副本引用同一个对象。我们看下面的引用类型的简历克隆的代码实现:
代码结构图:
![](https://www.cnblogs.com/images/cnblogs_com/peida/Prototype/resume1.png)
详细代码:
工作经历类:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
//工作经历
public class WorkExperience
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string workDate;
public string WorkDate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return workDate; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ workDate = value; }
}
private string company;
public string Company
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return company; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ company = value; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
简历类:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
//简历
public class Resume : ICloneable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string name;
private string sex;
private string age;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private WorkExperience work;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Resume(string name)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.name = name;
work = new WorkExperience();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置个人信息
public void SetPersonalInfo(string sex, string age)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.sex = sex;
this.age = age;
}
//设置工作经历
public void SetWorkExperience(string workDate, string company)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
work.WorkDate = workDate;
work.Company = company;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//显示
public void Display()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("{0} {1} {2}", name, sex, age);
Console.WriteLine("工作经历:{0} {1}", work.WorkDate, work.Company);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Object Clone()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (Object)this.MemberwiseClone();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
客户端:
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Resume a = new Resume("大鸟");
a.SetPersonalInfo("男", "29");
a.SetWorkExperience("1998-2000", "XX公司");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Resume b = (Resume)a.Clone();
b.SetWorkExperience("1998-2006", "YY企业");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Resume c = (Resume)a.Clone();
c.SetPersonalInfo("男", "24");
c.SetWorkExperience("1998-2003", "ZZ企业");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
a.Display();
b.Display();
c.Display();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.Read();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
下面我们看运行结果:
大鸟 男 29
工作经历 1998-2003 ZZ企业
大鸟 29
工作经历 1998-2003 ZZ企业
大鸟 男 24
工作经历 1998-2003 ZZ企业
由于MemberwiseClone()方法是浅表复制(克隆),对于值类型克隆没有问题,对于引用类型对象,只复制了引用,对引用的对象还是指向了原来的对象,所以就会出现我给a、b、c三个引用设置‘工作经历’,但却同时看到三个引用都是最后一次设置,因为三个引用都指向了同一个对象。
“浅复制”,被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。
“深复制”,深复制把引用对象的变量指向复制过的对象,而不是原有的被引用的对象。
下面来看深复制的实现:
代码结构图:
![](https://www.cnblogs.com/images/cnblogs_com/peida/Prototype/Resume2.jpg)
实现代码:
工作经验类:
//工作经历
public class WorkExperience : ICloneable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string workDate;
public string WorkDate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return workDate; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ workDate = value; }
}
private string company;
public string Company
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return company; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ company = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Object Clone()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (Object)this.MemberwiseClone();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
简历类:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
//简历
public class Resume : ICloneable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string name;
private string sex;
private string age;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private WorkExperience work;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Resume(string name)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.name = name;
work = new WorkExperience();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Resume(WorkExperience work)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.work = (WorkExperience)work.Clone();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置个人信息
public void SetPersonalInfo(string sex, string age)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.sex = sex;
this.age = age;
}
//设置工作经历
public void SetWorkExperience(string workDate, string company)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
work.WorkDate = workDate;
work.Company = company;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//显示
public void Display()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("{0} {1} {2}", name, sex, age);
Console.WriteLine("工作经历:{0} {1}", work.WorkDate, work.Company);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Object Clone()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Resume obj = new Resume(this.work);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
obj.name = this.name;
obj.sex = this.sex;
obj.age = this.age;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return obj;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
客户端代码与上面相同,执行结果:
大鸟 男 29
工作经历 1998-2000 XX公司
大鸟 29
工作经历 1998-2006 YY企业
大鸟 男 24
工作经历 1998-2003 ZZ企业
3. 总结
优缺点:
使用原型模式有以下优点:
(1)。在运行时增加或删除产品,只要通过客户端注册原型实例即可将新产品类型增加到系统中,例如组态软件中工具箱中的每个工具可以对应一个注册的原型对象,可以通过增加原型对象扩展工具箱。
(2)。很容易的创建复杂的对象:在图像编辑和组态等软件中,经常需要创建复杂的图元,这些图元是由简单的图元组成的,采用原型模式可以很容易的将复杂图元作为一般图元来使用,是软件的工具箱具有扩展功能。
(3)。减少工厂的层次:由于在.NET中可以使用反射工厂,因此这个优势并不明显。
使用原型模式的缺点:是在有些情况下克隆功能不容易实现,特别是在遇到对象的循环引用时。
在.NET中的很多类支持原型模式,例如我们希望获得一个与现有数据集(DataSet)结构相同的数据集,既可以采用克隆的方法。注意,DataSet有Clone()和Copy()两个方法,Clone()方法用来复制DataSet的结构,但不复制DataSet的数据,实现了原型模式的浅复制,Copy()方法,不但复制结构,也复制数据,实现了原型模式的深复制。