博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式学习笔记——原型模式(Prototype)
阅读量:5080 次
发布时间:2019-06-13

本文共 2750 字,大约阅读时间需要 9 分钟。

1.特点:在初始化信息不发生变化时考虑。

2.概念:通过复制一个已经存在的实例来创建一个新的实例。被复制的实例被称为原型,这个原型是可定制的。

3.类图:

4.程序实现:

///     /// 实现了ICloneable接口的简历类    ///     public class Resume:ICloneable    {        public Resume()        {            mWorkExperience = new WorkExperience();        }        private string mName;        private string mSex;        private int mAge;        private WorkExperience mWorkExperience;        public string Name        {            get { return mName; }            set { mName = value; }        }        public string Sex        {            get { return mSex; }            set { mSex = value; }        }        public int Age        {            get { return mAge; }            set { mAge = value; }        }        ///         /// 关联了一个引用类型        ///         public WorkExperience WorkExperience         {             get { return mWorkExperience; }         }        public void SetWorkExperience(DateTime startDate, DateTime endDate, string company, string position)        {            this.mWorkExperience.Company = company;            this.mWorkExperience.EndDate = endDate;            this.mWorkExperience.StartDate = startDate;            this.mWorkExperience.Position = position;        }        ///         /// 实现ICloneable接口的Clone方法        ///         /// 
public object Clone() { // .Net 为我们提供的浅复制对象的方法 return this.MemberwiseClone(); } } /// /// 工作经历类 /// public class WorkExperience { public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public string Company { get; set; } public string Position { get; set; } }[TestMethod] public void TestShallowCopy() { Resume myFirstResume = new Resume { Age = 29, Name = "Kevin Wang", Sex = "男", }; myFirstResume.SetWorkExperience(new DateTime(2006, 7, 1), new DateTime(2007, 7, 1), "My First Company", "Software Engineer"); Resume mySecondResume = (Resume)myFirstResume.Clone(); mySecondResume.SetWorkExperience(new DateTime(2007, 8, 1), new DateTime(2008, 8, 1), "My Second Company", "Software Engineer"); Resume myThirdResume = (Resume)myFirstResume.Clone(); myThirdResume.SetWorkExperience(new DateTime(2008, 8, 1), new DateTime(2009, 8, 1), "My Third Company", "Senior Software Engineer"); Assert.AreEqual("My First Company", myFirstResume.WorkExperience.Company); Assert.AreEqual("My Second Company", mySecondResume.WorkExperience.Company); Assert.AreEqual("My Third Company", myThirdResume.WorkExperience.Company); }

  

转载于:https://www.cnblogs.com/ice-baili/p/4729205.html

你可能感兴趣的文章
spring boot配置跨域
查看>>
BZOJ 1996 合唱队(DP)
查看>>
进击吧!阶乘——大数乘法
查看>>
安卓学习资料推荐-25
查看>>
Mysql数据库备份和还原常用的命令
查看>>
关于退出当前页面在火狐的一些问题
查看>>
【项目实施】项目考核标准
查看>>
spring-aop AnnotationAwareAspectJAutoProxyCreator类
查看>>
经典入门_排序
查看>>
Redis Cluster高可用集群在线迁移操作记录【转】
查看>>
二、spring中装配bean
查看>>
VIM工具
查看>>
javascript闭包
查看>>
@Column标记持久化详细说明
查看>>
创建本地yum软件源,为本地Package安装Cloudera Manager、Cloudera Hadoop及Impala做准备...
查看>>
mysql8.0.13下载与安装图文教程
查看>>
站立会议08(冲刺2)
查看>>
url查询参数解析
查看>>
http://coolshell.cn/articles/10910.html
查看>>
[转]jsbsim基础概念
查看>>