原型模式(Prototype):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
1、原型模式就是从一个对象在创建另外一个可定制的对象,而且不需要知道任何创建的细节。
2、.NET在System命名空间中提供了Icloneable接口,其中就有唯一的一个方法Clone(),实现这个接口就可以完成原型模式了。
3、浅复制:被复制对象的所有变量都含有与原来对象相同的值,而所有对其他对象的引用都指向原来的对象。
4、深复制:把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Prototype
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 Resume liuwei = new Resume("liuwei");
12 liuwei.SetPersonalInfo("man","26");
13 liuwei.SetWorkExperience("2007--2008","xxx company");
14
15 Resume liuweiClone =(Resume)liuwei.Clone();
16 liuweiClone.SetWorkExperience("2008--","yyy company");
17
18 liuwei.Display();
19 liuweiClone.Display();
20
21 Console.ReadLine();
22 }
23 }
24 class WorkExperience:ICloneable
25 {
26 private string workDate;
27
28 public string WorkDate
29 {
30 get { return workDate; }
31 set { workDate = value; }
32 }
33 private string workCompany;
34
35 public string WorkCompany
36 {
37 get { return workCompany; }
38 set { workCompany = value; }
39 }
40 public Object Clone()
41 {
42 return (Object)this.MemberwiseClone();
43 }
44
45 }
46 class Resume:ICloneable
47 {
48 private string name;
49 private string sex;
50 private string age;
51 private WorkExperience work;
52 public Resume(string name)
53 {
54 this.name = name;
55 work = new WorkExperience();
56 }
57 private Resume(WorkExperience work)
58 {
59 this.work = (WorkExperience)work.Clone();
60 }
61 //设置个人信息
62 public void SetPersonalInfo(string sex,string age)
63 {
64 this.sex = sex;
65 this.age = age;
66 }
67 //设置工作经历
68 public void SetWorkExperience(string workDate,string company)
69 {
70 work.WorkDate = workDate;
71 work.WorkCompany= company;
72 }
73 //显示
74 public void Display()
75 {
76 Console.WriteLine("{0} {1} {2}", name, sex, age);
77 Console.WriteLine("工作经历:{0} {1}",work.WorkDate,work.WorkCompany);
78 }
79 public Object Clone()
80 {
81 Resume obj = new Resume(this.work);
82 obj.name = this.name;
83 obj.sex = this.sex;
84 obj.age = this.age;
85 return obj;
86 }
87
88 }
89 }
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Prototype
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 Resume liuwei = new Resume("liuwei");
12 liuwei.SetPersonalInfo("man","26");
13 liuwei.SetWorkExperience("2007--2008","xxx company");
14
15 Resume liuweiClone =(Resume)liuwei.Clone();
16 liuweiClone.SetWorkExperience("2008--","yyy company");
17
18 liuwei.Display();
19 liuweiClone.Display();
20
21 Console.ReadLine();
22 }
23 }
24 class WorkExperience:ICloneable
25 {
26 private string workDate;
27
28 public string WorkDate
29 {
30 get { return workDate; }
31 set { workDate = value; }
32 }
33 private string workCompany;
34
35 public string WorkCompany
36 {
37 get { return workCompany; }
38 set { workCompany = value; }
39 }
40 public Object Clone()
41 {
42 return (Object)this.MemberwiseClone();
43 }
44
45 }
46 class Resume:ICloneable
47 {
48 private string name;
49 private string sex;
50 private string age;
51 private WorkExperience work;
52 public Resume(string name)
53 {
54 this.name = name;
55 work = new WorkExperience();
56 }
57 private Resume(WorkExperience work)
58 {
59 this.work = (WorkExperience)work.Clone();
60 }
61 //设置个人信息
62 public void SetPersonalInfo(string sex,string age)
63 {
64 this.sex = sex;
65 this.age = age;
66 }
67 //设置工作经历
68 public void SetWorkExperience(string workDate,string company)
69 {
70 work.WorkDate = workDate;
71 work.WorkCompany= company;
72 }
73 //显示
74 public void Display()
75 {
76 Console.WriteLine("{0} {1} {2}", name, sex, age);
77 Console.WriteLine("工作经历:{0} {1}",work.WorkDate,work.WorkCompany);
78 }
79 public Object Clone()
80 {
81 Resume obj = new Resume(this.work);
82 obj.name = this.name;
83 obj.sex = this.sex;
84 obj.age = this.age;
85 return obj;
86 }
87
88 }
89 }