原型模式就是从一个对象在创建另外一个对象,不需要知道任何创建的细节:(克隆)
以创建简历为例:
简历类(Resume) :继承于系统的克隆接口(ICloneable)
namespace Ptototype_Pattern { class ReSume:ICloneable { private string name; private string age; private string sex; private string birthday; private string time; private string company; public ReSume(string name) { this.name = name; } /// <summary> /// 设置个人信息 /// </summary> /// <param name="age"></param> /// <param name="sex"></param> /// <param name="birthday"></param> public void SetPersonInfo(string age, string sex, string birthday) { this.age = age; this.sex = sex; this.birthday = birthday; } /// <summary> /// 设置工作经历 /// </summary> /// <param name="time"></param> /// <param name="company"></param> public void SetWorkExperice(string time, string company) { this.time = time; this.company = company; } public void Display() { Console.WriteLine("姓名:{0} 年龄:{1} 出生日期:{2}", name, age, birthday); Console.WriteLine("在 {0} 工作于 {1} ",time,company); } public Object Clone() { return (Object)this.MemberwiseClone(); } } }
客户端类(TestMain)
namespace Ptototype_Pattern { class TestMain { static void Main(string[] args) { ReSume A = new ReSume("张三"); A.SetPersonInfo("25","男","1988-10-10"); A.SetWorkExperice("2010--2013", "X公司"); ReSume B = (ReSume)A.Clone(); B.SetWorkExperice("2009--2010", "Y公司"); ReSume C = (ReSume)A.Clone(); C.SetWorkExperice("2006--2009", "Z公司"); A.Display(); B.Display(); C.Display(); Console.ReadLine(); } } }
深复制和浅复制:MemberwiseClone 方法创建一个浅表副本,方法是创建一个新对象,然后将当前对象的非静态字段复制到该新对象。 如果字段是值类型的,则对该字段执行逐位复制。 如果字段是引用类型,则复制引用但不复制引用的对象;因此,原始对象及其复本引用同一对象。
引用类型的例子如下:
新添加工作经历类(WorkExperice)
namespace Ptototype_Pattern { class WorkExperice { private string workTime; private string workCompany; /// <summary> /// 工作时间 /// </summary> public string WorkTime { get { return workTime; } set { workTime = value; } } /// <summary> /// 工作企业 /// </summary> public string WorkCompany { get { return workCompany; } set { workCompany = value; } } } }
简历类(Resume) :继承于系统的克隆接口(ICloneable)
namespace Ptototype_Pattern { class ReSume:ICloneable { private string name; private string age; private string sex; private string birthday; //private string time; //private string company; private WorkExperice work; public ReSume(string name) { this.name = name; work = new WorkExperice(); } /// <summary> /// 设置个人信息 /// </summary> /// <param name="age"></param> /// <param name="sex"></param> /// <param name="birthday"></param> public void SetPersonInfo(string age, string sex, string birthday) { this.age = age; this.sex = sex; this.birthday = birthday; } /// <summary> /// 设置工作经历 /// </summary> /// <param name="time"></param> /// <param name="company"></param> public void SetWorkExperice(string time, string company) { //this.time = time; //this.company = company; work.WorkTime = time; work.WorkCompany = company; } public void Display() { //Console.WriteLine("姓名:{0} 年龄:{1} 出生日期:{2}", name, age, birthday); //Console.WriteLine("在 {0} 工作于 {1} ",time,company); Console.WriteLine("姓名:{0} 年龄:{1} 出生日期:{2}", name, age, birthday); Console.WriteLine("在 {0} 工作于 {1} ", work.WorkTime, work.WorkCompany); } public Object Clone() { return (Object)this.MemberwiseClone(); } } }
客户端不变:(全为Z公司)
MemberwiseClone 方法创建一个浅表副本,对于引用类型复制引用,但不复制引用的对象
改为深复制如下:
工作经历类(WorkExperice)
namespace Ptototype_Pattern { class WorkExperice:ICloneable { private string workTime; private string workCompany; /// <summary> /// 工作时间 /// </summary> public string WorkTime { get { return workTime; } set { workTime = value; } } /// <summary> /// 工作企业 /// </summary> public string WorkCompany { get { return workCompany; } set { workCompany = value; } } public Object Clone() { return (object)this.MemberwiseClone(); } } }
简历类(Resume) :继承于系统的克隆接口(ICloneable)
namespace Ptototype_Pattern { class ReSume:ICloneable { private string name; private string age; private string sex; private string birthday; //private string time; //private string company; private WorkExperice work; public ReSume(string name) { this.name = name; work = new WorkExperice(); } public ReSume(WorkExperice work) { this.work = (WorkExperice)work.Clone(); } /// <summary> /// 设置个人信息 /// </summary> /// <param name="age"></param> /// <param name="sex"></param> /// <param name="birthday"></param> public void SetPersonInfo(string age, string sex, string birthday) { this.age = age; this.sex = sex; this.birthday = birthday; } /// <summary> /// 设置工作经历 /// </summary> /// <param name="time"></param> /// <param name="company"></param> public void SetWorkExperice(string time, string company) { //this.time = time; //this.company = company; work.WorkTime = time; work.WorkCompany = company; } public void Display() { //Console.WriteLine("姓名:{0} 年龄:{1} 出生日期:{2}", name, age, birthday); //Console.WriteLine("在 {0} 工作于 {1} ",time,company); Console.WriteLine("姓名:{0} 年龄:{1} 出生日期:{2}", name, age, birthday); Console.WriteLine("在 {0} 工作于 {1} ", work.WorkTime, work.WorkCompany); } public Object Clone() { //return (Object)this.MemberwiseClone(); ReSume resum = new ReSume(this.work); resum.name = this.name; resum.age = this.age; resum.birthday = this.birthday; return resum; } } }
客户端不变: