原型模式之深浅复制

时间:2022-09-09 09:06:13

前言

    原型模式也写过一篇博客《原型模式》,但是深浅复制没有细讲,这一次学习原型时,深浅复制虽然思想清楚,但对应到代码时却糊涂了,经过与它人交流讨论与思考,已明白其原理。

浅复制

<span style="font-size:18px;">    class WordExperience:ICloneable
{
private string workDate;
public string WorkDate
{
get { return workDate; }
set { workDate = value; }
}
private string company;
public string company
{
get { return company; }
set { company= value; }
}

}

//简历
class Resume :ICloneable
{
private string name;
private string sex;
private string age;

private WorkExperience work;


public Resume(string name)
{
this.name = name;
work = new WorkExperience();
}
public void SetPersonalInfo(string sex, string age)
{
this.sex = sex;
this.age = age;
}
//工作经历
public void SetWorkExperience(string workDate, string company)
{
this.WorkDate = workDate;
this.company = company;
}
//显示
public void Display()
{
Console.WriteLine("{0} {1} {2}", name, sex, age);
Console.WriteLine("工作经历:{0},{1}", work.WorkDate, work.company);
}
//克隆方法
public Object Clone()
{
return (Object)this.MemberwiseClone();
}
}
//客户端代码:
class Program
{
static void Main(string[] args)
{
//定义一个简历类(原型类)
Resume a = new Resume("大鸟");
a.SetPersonalInfo("男", "29");
a.SetWorkExperience("1998-2000", "XX公司");


Resume b = (Resume)a.Clone(); //克隆一个a对象
b.SetWorkExperience("1998-2006", "YY公司"); //重写b对象方法
Resume c = (Resume)a.Clone(); //再次克隆一个a对象
c.SetPersonalInfo("男", "24"); //重写c对象方法
//显示
a.Display();
b.Display();
c.Display();

Console.Read();
}
}</span>


    重点在克隆方法MemberwiseClone():如果字段是值类型的,则对该字段执行诸位复制,如果字段是引用类型,则复制引用不复制对象,因此,原始对象及其复本引用同一个对象,这叫浅复制,被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其它对象的引用都仍然指向原来的对象。然而深复制是把引用的对象的变量指向复制过的新对象,而不是原有的被引用的对象。

<pre name="code" class="java">class WordExperience:ICloneable    {       private string workDate;       public string WorkDate       {          get { return workDate; }          set { workDate = value; }       }       private string company;       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 string age;                private WorkExperience work;          public Resume(string name)        {            this.name = name;            work = new WorkExperience();   //构造函数运行时,会在内存的堆中克隆出一个新的对象        }        pulbic Resume(WorkExperience work)        {            this.work=(WorkExperience)work.Clone();        }         public void SetPersonalInfo(string sex, string age)        {            this.sex = sex;            this.age = age;        }        //工作经历        public void SetWorkExperience(string workDate, string company)        {            this.WorkDate = workDate;            this.company = company;        }       //显示        public void Display()        {            Console.WriteLine("{0} {1} {2}", name, sex, age);            Console.WriteLine("工作经历:{0},{1}", work.WorkDate, work.company);        }       //深复制克隆的实现。这个方法能克隆出简历类对象       public Object Clone()       {            Resume obj = new Resume(this.work);//工作经验类具体克隆在构造函数里            obj.name = this.name;            obj.sex = this.sex;            obj.age = this.age;            return obj;       }    }



深浅复制内存分析简单对比:
原型模式之深浅复制
原型模式之深浅复制