0%

设计模式-原型模式

原型模式是用于创建重复的对象,同时又能保证性能, 当直接创建对象的代价比较大时例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//普通实现
class Resume
{
private string name;
private string sex;
private string age;
private string timeArea;
private string compart;

public Resume(string name)
{
this.name = name;
}
public void SetPersonalInfo(string sex, string age)
{
this.sex = sex;
this.age = age;
}
public void SetWorkExperience(string timeArea, string company)
{
this.timeArea = timeArea;
this.company = company;
}
public void Display()
{
Console.WriteLine("{0} {1} {2}", name, sex, age);
Console.WriteLine("工作经历: {0} {1}", timeArea, company);
}
}
//Client---需要多次创建对象
Resume a = new Resume("小a");
a.SetPersonalInfo("男", "21");
a.SetWorkExperience("2021-2022", "a公司");
Resume b = new Resume("小a");
b.SetPersonalInfo("男", "21");
b.SetWorkExperience("2022-2023", "b公司");

a.Display();
b.Display();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//原型模式
class Resume : ICloneable
{
private string name;
private string sex;
private string age;
private string timeArea;
private string compart;

public Resume(string name)
{
this.name = name;
}
public void SetPersonalInfo(string sex, string age)
{
this.sex = sex;
this.age = age;
}
public void SetWorkExperience(string timeArea, string company)
{
this.timeArea = timeArea;
this.company = company;
}
public void Display()
{
Console.WriteLine("{0} {1} {2}", name, sex, age);
Console.WriteLine("工作经历: {0} {1}", timeArea, company);
}
public Object Clone()
{
//浅拷贝--值类型:逐位复制, 引用类型只复制引用(指向同一对象)
return (Object)this.MemberwiseClone();
}
}
//Client
Resume a = new Resume("小a");
a.SetPersonalInfo("男", "21");
a.SetWorkExperience("2021-2022", "a公司");
Resume b = (Resume)a.Clone();
b.SetWorkExperience("2022-2023", "b公司");

a.Display();
b.Display();

浅拷贝存在的问题: 对于引用类型只是拷贝其引用, 创建多个对象后引用类型都指向同一对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class WorkExperence
{
private string workDate;
public string WorkDate
{
get{return workDate;}
set{workDate = value;}
}
public string company;
}
class Resume : ICloneable
{
private string name;
private string sex;
private string age;
private WorkExperence work;

public Resume(string name)
{
this.name = name;
work = new WorkExperence();
}
public void SetPersonalInfo(string sex, string age)
{
this.sex = sex;
this.age = age;
}
public void SetWorkExperience(string timeArea, string company)
{
work.WorkDate = timeArea;
work.Company = company;
}
public void Display()
{
Console.WriteLine("{0} {1} {2}", name, sex, age);
Console.WriteLine("工作经历: {0} {1}", timeArea, company);
}
public Object Clone()
{
//浅拷贝--值类型:逐位复制, 引用类型只复制引用(指向同一对象)
return (Object)this.MemberwiseClone();
}
}
//Client
Resume a = new Resume("小a");
a.SetPersonalInfo("男", "21");
a.SetWorkExperience("2021-2022", "a公司");
Resume b = (Resume)a.Clone();
b.SetWorkExperience("2022-2023", "b公司");//---引用类型重新赋值会覆盖原有值

a.Display();
b.Display();

深拷贝需要考虑要深入到多少层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//深拷贝
class WorkExperence : ICloneable
{
private string workDate;
public string WorkDate
{
get{return workDate;}
set{workDate = value;}
}
public string company;
public Object Clone()
{
//浅拷贝--值类型:逐位复制, 引用类型只复制引用(指向同一对象)
return (Object)this.MemberwiseClone();
}
}
class Resume : ICloneable
{
private string name;
private string sex;
private string age;
private WorkExperence work;

public Resume(string name)
{
this.name = name;
work = new WorkExperence();
}
public Resume(WorkExperence work)
{
this.work = (WorkExperence)work.Clone();
}
public void SetPersonalInfo(string sex, string age)
{
this.sex = sex;
this.age = age;
}
public void SetWorkExperience(string timeArea, string company)
{
work.WorkDate = timeArea;
work.Company = company;
}
public void Display()
{
Console.WriteLine("{0} {1} {2}", name, sex, age);
Console.WriteLine("工作经历: {0} {1}", timeArea, company);
}
public Object Clone()
{
//深拷贝
Resume obj = new Resume(this.work);
obj.name = this.name;
obj.sex = this.sex;
obj.age = this.age;
return obj;
}
}
//Client
Resume a = new Resume("小a");
a.SetPersonalInfo("男", "21");
a.SetWorkExperience("2021-2022", "a公司");
Resume b = (Resume)a.Clone();
b.SetWorkExperience("2022-2023", "b公司");

a.Display();
b.Display();