设计模式之浅复制与深复制
浅复制
-
class Resume:ICloneable
-
{
-
private string UserName;
-
private string Sex;
-
private int Age;
-
private string Company;
-
private string TimeArea;
-
public Resume(string Name)
-
{
-
this.UserName = Name;
-
}
-
-
public void SetPerson(string Sex, int Age)
-
{
-
this.Sex = Sex;
-
this.Age = Age;
-
}
-
-
public void SetWorkExperience(string Company, string TimeArea)
-
{
-
this.Company = Company;
-
this.TimeArea = TimeArea;
-
}
-
-
public void Display()
-
{
-
Console.WriteLine("{0}
{1} {2}", UserName, Sex, Work.Age);
-
Console.WriteLine("工作经历:{1} {0}",Company,TimeArea);
-
}
-
-
-
-
-
public object Clone()
-
{
-
return (Object)this.MemberwiseClone();
-
}
-
static void Main()
-
{
-
Resume a = new Resume("大鸟");
-
a.SetPeraonalInfo("男", "29");
-
a.SetWorkExperience("1998-2000", "东南角公司");
-
-
Resume b = (Resume)a.Clone();
-
b.SetWorkExperience("2001-2006", "电视剧公司");
-
-
Resume c = (Resume)a.Clone();
-
c.SetWorkExperience("2007-至今", "份额公司");
-
-
a.DisPlay();
-
b.DisPlay();
-
c.DisPlay();
-
-
-
}
-
}
结果:

程序中涉及到的字段类型是值类型,对该字段进行逐位复制,但是如果字段涉及到引用类型的话,则复制引用但不复制引用中的对象。
比如程序中需要加入一个工作经历类,这样再用浅复制就不合适了
下面我们还是用上面的方法将工作经历抽象成一个方法对比一下
代码如下
-
-
class WorkExperience
-
{
-
private string company;
-
public string Company
-
{
-
set { company = value; }
-
get { return company; }
-
}
-
private string timeArea;
-
public string TimeArea
-
{
-
set { timeArea = value; }
-
get { return timeArea; }
-
}
-
}
-
class Resume:ICloneable
-
{
-
private string UserName;
-
private string Sex;
-
private int Age;
-
private WorkExperience Work;
-
public Resume(string Name)
-
{
-
this.UserName = Name;
-
Work = new WorkExperience();
-
}
-
-
public void SetPerson(string Sex, int Age)
-
{
-
this.Sex = Sex;
-
this.Age = Age;
-
}
-
-
public void SetWorkExperience(string Company, string TimeArea)
-
{
-
Work.Company = Company;
-
Work.TimeArea = TimeArea;
-
}
-
-
public void Display()
-
{
-
Console.WriteLine("{0}
{1} {2}", UserName, Sex, Work.Age);
-
Console.WriteLine("工作经历:{1} {0}", Work.Company, Work.TimeArea);
-
}
-
-
-
-
-
public object Clone()
-
{
-
return (Object)this.MemberwiseClone();
-
}
-
static void Main()
-
{
-
Resume a = new Resume("大鸟");
-
a.SetPerson("男", 29);
-
a.SetWorkExperience("1998-2000", "东南角公司");
-
Resume b = (Resume)a.Clone();
-
b.SetWorkExperience("2001-2006", "电视剧公司");
-
Resume c = (Resume)a.Clone();
-
c.SetWorkExperience("2007-至今", "2010-份额公司");
-
a.Display();
-
b.Display();
-
c.Display();
-
}
-
}
结果:

运行结果只显示了最后一个工作经历,显然浅复制就不能满足客户的需求了,那么我们就要深复制来解决这个问题
深复制是把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象
深复制
代码如下:
-
-
class WorkExperience:ICloneable
-
{
-
private string company;
-
public string Company
-
{
-
set { company = value; }
-
get { return company; }
-
}
-
private string timeArea;
-
public string TimeArea
-
{
-
set { timeArea = value; }
-
get { return timeArea; }
-
}
-
private int age;
-
public int Age
-
{
-
set { age = value; }
-
get { return age; }
-
}
-
-
-
-
-
public object Clone()
-
{
-
return (Object)this.MemberwiseClone();
-
}
-
}
-
class Resume:ICloneable
-
{
-
private string UserName;
-
private string Sex;
-
-
private WorkExperience Work;
-
public Resume(string Name)
-
{
-
this.UserName = Name;
-
Work = new WorkExperience();
-
}
-
-
private Resume(WorkExperience Work)
-
{
-
this.Work = (WorkExperience)Work.Clone();
-
}
-
-
public void SetPerson(string Sex)
-
{
-
this.Sex = Sex;
-
-
}
-
-
public void SetWorkExperience(string Company, string TimeArea, int Age)
-
{
-
Work.Company = Company;
-
Work.TimeArea = TimeArea;
-
Work.Age = Age;
-
}
-
-
public void Display()
-
{
-
Console.WriteLine("{0} {1} {2}", UserName, Sex, Work.Age);
-
Console.WriteLine("工作经历:{1} {0}", Work.Company, Work.TimeArea);
-
}
-
-
-
-
-
public Object Clone()
-
{
-
Resume obj = new Resume(this.Work);
-
obj.UserName = this.UserName;
-
obj.Sex = this.Sex;
-
return obj;
-
}
-
static void Main()
-
{
-
Resume a = new Resume("大鸟");
-
a.SetPerson("男","29");
-
a.SetWorkExperience("1998-2000", "东南角公司");
-
Resume b = (Resume)a.Clone();
-
b.SetWorkExperience("2001-2006", "电视剧公司");
-
Resume c = (Resume)a.Clone();
-
c.SetWorkExperience("2007-至今", "份额公司");
-
a.Display();
-
b.Display();
-
c.Display();
-
}
-
}
结果:

通过运行的结果,相信大家就能很清楚的对比出他们的区别了,也就是说如果你想克隆引用类型的字段,那么就用深复制来解决,如果你的字段中只用值类型,则可以选用浅复制,比如说:数据集对象DataSet,它就有Clone()方法和Copy()方法,Clone()方法用来复制DataSet的数据,而不复制DataSet的数据,实现了原型模式的浅复制;Copy()方法不但复制结构,也复制数据,也就是实现了原型模式的深复制。