设计模式笔记(七) —— 原型模式

原型模式(Prototype):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。原型模式其实就是从一个对象再创建另一个可以定制的对象,而且不需要知道任何创建的细节。
     浅复制:被复制对象的所有变量都含有与原来的对象相同的值,而所有的其它的对象的引用都仍然指向原来的对象。
     深复制:把引用对象的变量指向复制过来的新对象,而不是原有对象的引用。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace StuDesignMode.Prototype
  6. {
  7.     /// <summary>
  8.     /// 工作经历
  9.     /// </summary>
  10.     class WorkExperience:ICloneable
  11.     {
  12.         /// <summary>
  13.         /// 时间段
  14.         /// </summary>
  15.         public string WorkDate { getset; }
  16.         /// <summary>
  17.         /// 公司名称
  18.         /// </summary>
  19.         public string Company { getset; }
  20.     
  21.         public object  Clone()
  22.         {
  23.             return this.MemberwiseClone();
  24.         }
  25. }
  26.     /// <summary>
  27.     /// 简历
  28.     /// </summary>
  29.     class Resume : ICloneable
  30.     {
  31.         public string Name { getset; }
  32.         public string Sex { getset; }
  33.         public string Age { getset; }
  34.         public WorkExperience workExperience { getset; }
  35.         public Resume(string name)
  36.         {
  37.             this.Name = name;
  38.             this.workExperience = new WorkExperience();
  39.         }
  40.         private Resume(WorkExperience work)
  41.         {
  42.             this.workExperience = (WorkExperience)work.Clone();
  43.         }
  44.         /// <summary>
  45.         /// 设置个人信息
  46.         /// </summary>
  47.         /// <param name="sex"></param>
  48.         /// <param name="age"></param>
  49.         public void SetPersonlInfo(string name,string sex, string age)
  50.         {
  51.             this.Name = name;
  52.             this.Sex = sex;
  53.             this.Age = age;
  54.         }
  55.         /// <summary>
  56.         /// 设置工作经历
  57.         /// </summary>
  58.         /// <param name="workDate"></param>
  59.         /// <param name="company"></param>
  60.         public void setWorkExperience(string workDate, string company)
  61.         {
  62.             this.workExperience.WorkDate = workDate;
  63.             this.workExperience.Company = company;
  64.         }
  65.         /// <summary>
  66.         /// 显示
  67.         /// </summary>
  68.         public void Display()
  69.         {
  70.             Console.WriteLine("{0} {1} {2}"this.Name, this.Sex, this.Age);
  71.             Console.WriteLine("工作经历:{0} {1}", workExperience.WorkDate, workExperience.Company);
  72.         }
  73.         public object Clone()
  74.         {
  75.             Resume resume = new Resume(this.workExperience);
  76.             resume.Name = this.Name;
  77.             resume.Age = this.Age;
  78.             resume.Sex = this.Sex;
  79.             return resume;
  80.         }
  81.     }
  82.     public class ClientTest
  83.     {
  84.         static void Main(string[] args)
  85.         {
  86.             Resume liming = new Resume("李明");
  87.             liming.SetPersonlInfo("李明""男""22岁");
  88.             liming.setWorkExperience("2005年 --- 2007年 ""IBM");
  89.             liming.Display();
  90.             Resume wanghao = liming.Clone() as Resume;
  91.             wanghao.SetPersonlInfo("王浩""男""24岁");
  92.             wanghao.setWorkExperience("2005年 --- 2008年 ""Sun");
  93.             wanghao.Display();
  94.         }
  95.     }
  96. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值