首页 / 设计模式 / 《大话设计模式》学习笔记6:原型模式
《大话设计模式》学习笔记6:原型模式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了《大话设计模式》学习笔记6:原型模式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2697字,纯文字阅读大概需要4分钟。
内容图文
说明:1..NET在System命名空间中提供了ICloneable接口,其中只有唯一的一个方法Clone(),只需要实现这个接口就可以完成原型模式。【即上图中Prototype用ICloneable代替】
2.MemberwiseClone():, and then copying the nonstatic fields of the current object to the new object.">创建一个浅表副本,方法是创建一个新对象,然后将当前对象的非静态字段复制到该新对象。 如果字段是值类型的,则对该字段执行逐位复制。 如果字段是引用类型,则复制引用但不复制引用的对象;因此,原始对象及其复本引用同一对象。【对于内部的Class对象和数组,则Copy一份地址;而对于其它内置的int/string/enum/struct/object类型,则Copy一份值(参考:C# 浅拷贝与深拷贝区别)】
, and then copying the nonstatic fields of the current object to the new object.">一、简历复印示例:
, and then copying the nonstatic fields of the current object to the new object.">
, and then copying the nonstatic fields of the current object to the new object.">1.具体原型类:
public class Resume:ICloneable { private string name; private string timeArea; private string company; public Resume(string name) { this.name = name; } publicvoid SetWorkExperience(string timeArea,string company) { this.timeArea = timeArea; this.company = company; } publicvoid Display() { Console.WriteLine("{0} 工作经历:{1} {2}", name,timeArea, company); } publicobject Clone() { return (object)this.MemberwiseClone(); } }
2.客户端调用:
class Program { static void Main(string[] args) { Resume a = new Resume("a"); a.SetWorkExperience("1998-2000", "X公司"); Resume b = (Resume)a.Clone(); b.SetWorkExperience("1998-2006", "Y公司"); a.Display(); b.Display(); } }
二、简历的深复制示例:
说明:参考上图若Resume类中包含WorkExperience类,则在上面的[简历复印示例]中,Resume复制对WorkExperience的复制为浅复制,不同的Resume对象对WorkExperience指向同一个引用。
1.原型包含类:
public class WorkExperience:ICloneable { public string WorkDate { get; set; } publicstring Company { get; set; } publicobject Clone() { return (object)this.MemberwiseClone(); } }
2.具体原型类:
public class Resume:ICloneable { private string name; private WorkExperience workExperience; public Resume(string name) { this.name = name; workExperience = new WorkExperience(); } //提供Clone方法调用的私有构造函数,以便克隆“工作经历”的数据private Resume(WorkExperience workExperience) { this.workExperience = (WorkExperience)workExperience.Clone(); } publicvoid SetWorkExperience(string workDate,string company) { workExperience.WorkDate = workDate; workExperience.Company = company; } publicvoid Display() { Console.WriteLine("{0} 工作经历:{1} {2}", name,workExperience.WorkDate, workExperience.Company); } publicobject Clone() { Resume resume = new Resume(this.workExperience); resume.name = this.name; return resume; } }
3.客户端调用:
class Program { static void Main(string[] args) { Resume a = new Resume("a"); a.SetWorkExperience("1998-2000", "X公司"); Resume b = (Resume)a.Clone(); b.SetWorkExperience("1998-2006", "Y公司"); a.Display(); b.Display(); } }
原型模式其实就是从一个对象再创建另外一个可定制的对象,而不需要知道任何创建的细节。
原文:http://www.cnblogs.com/walden1024/p/4494790.html
内容总结
以上是互联网集市为您收集整理的《大话设计模式》学习笔记6:原型模式全部内容,希望文章能够帮你解决《大话设计模式》学习笔记6:原型模式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。