1. 概述
通過(guò)復(fù)制一個(gè)已經(jīng)存在的實(shí)例來(lái)創(chuàng)建一個(gè)新的實(shí)例。被復(fù)制的實(shí)例被稱(chēng)為原型,這個(gè)原型是可定制的。
2. 模式中的角色
2.1 抽象原型類(lèi)(abstract prototype):提供一個(gè)克隆接口
2.2 具體原型類(lèi)(concrete prototype): 及實(shí)現(xiàn)了克隆接口的具體原型類(lèi)
3. 實(shí)例:求職網(wǎng)站上現(xiàn)在都支持多份簡(jiǎn)歷,如果每創(chuàng)建一份簡(jiǎn)歷都要從頭至尾地填寫(xiě)一遍,那也是非常讓人沮喪的事。其實(shí)針對(duì)我們的求職崗位的不同,不同的簡(jiǎn)歷可能只要修改局部?jī)?nèi)容就可以了,而不用全部重新構(gòu)建一份新的簡(jiǎn)歷。復(fù)制一份簡(jiǎn)歷,然后做局部修改是最讓人省心的了!
3.1 實(shí)現(xiàn)類(lèi)圖
類(lèi)圖解讀
在.net中,system命名空間已經(jīng)為我們提供了一個(gè)icloneable接口,它包含了一個(gè)方法clone(),實(shí)現(xiàn)這個(gè)接口就完成了原型模式。
3.2 在寫(xiě)實(shí)現(xiàn)代碼之前,先要理解一下深復(fù)制與淺復(fù)制。
3.2.1 淺復(fù)制:將原來(lái)對(duì)象中的所有字段逐個(gè)復(fù)制到一個(gè)新對(duì)象,如果字段是值類(lèi)型,則簡(jiǎn)單地復(fù)制一個(gè)副本到新對(duì)象,改變新對(duì)象的值類(lèi)型字段不會(huì)影響原對(duì)象;如果字段是引用類(lèi)型,則復(fù)制的是引用,改變目標(biāo)對(duì)象中引用類(lèi)型字段的值將會(huì)影響原對(duì)象。例如, 如果一個(gè)對(duì)象有一個(gè)指向引用類(lèi)型(如例子中的工作經(jīng)歷)的字段, 并且我們對(duì)該對(duì)象做了一個(gè)淺復(fù)制, 那麼兩個(gè)對(duì)象將引用同一個(gè)引用(即同一段工作經(jīng)歷)。
3.2.2 深復(fù)制:與淺復(fù)制不同之處在于對(duì)引用類(lèi)型的處理,深復(fù)制將新對(duì)象中引用類(lèi)型字段指向復(fù)制過(guò)的新對(duì)象,改變新對(duì)象中引用的任何對(duì)象,不會(huì)影響到原來(lái)的對(duì)象中對(duì)應(yīng)字段的內(nèi)容。例如,如果一個(gè)對(duì)象有一個(gè)指向引用類(lèi)型(如例子中的工作經(jīng)歷)的字段,并且對(duì)該對(duì)象做了一個(gè)深復(fù)制的話(huà).我門(mén)將創(chuàng)建一個(gè)新的對(duì)象(即新的工作經(jīng)歷)。
3.3 簡(jiǎn)歷的淺復(fù)制實(shí)現(xiàn)
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
67
68
69
70
|
/// <summary> /// 實(shí)現(xiàn)了icloneable接口的簡(jiǎn)歷類(lèi) /// </summary> 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; } } /// <summary> /// 關(guān)聯(lián)了一個(gè)引用類(lèi)型 /// </summary> 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; } /// <summary> /// 實(shí)現(xiàn)icloneable接口的clone方法 /// </summary> /// <returns></returns> public object clone() { // .net 為我們提供的淺復(fù)制對(duì)象的方法 return this .memberwiseclone(); } } /// <summary> /// 工作經(jīng)歷類(lèi) /// </summary> public class workexperience { public datetime startdate { get ; set ; } public datetime enddate { get ; set ; } public string company { get ; set ; } public string position { get ; set ; } } |
下面是測(cè)試代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[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); } |
這里期望的是三個(gè)斷言都能運(yùn)行成功,但是卻是失敗的,原因是:由于我們使用的是淺復(fù)制,所以myfirstresume, mysecondresume 和 mythirdresume引用的是同一個(gè)對(duì)象,因此最終的結(jié)果是 三個(gè)簡(jiǎn)歷的workexperience.company都是“my third company".
3.4 簡(jiǎn)歷的深復(fù)制實(shí)現(xiàn)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
/// <summary> /// 實(shí)現(xiàn)了icloneable接口的簡(jiǎn)歷類(lèi) /// </summary> public class resume : icloneable { public resume() { mworkexperience = new workexperience(); } /// <summary> /// 這里使用一個(gè)私有的構(gòu)造函數(shù)來(lái)對(duì)其連接到的引用類(lèi)型進(jìn)行復(fù)制 /// </summary> /// <param name="workexperience"></param> private resume(workexperience workexperience) { this .mworkexperience = (workexperience)workexperience.clone(); } 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; } } /// <summary> /// 設(shè)置功過(guò)經(jīng)歷 /// </summary> /// <param name="startdate"></param> /// <param name="enddate"></param> /// <param name="company"></param> /// <param name="position"></param> 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; } /// <summary> /// 實(shí)現(xiàn)icloneable接口的clone方法 /// </summary> /// <returns></returns> public object clone() { // 這里不再使用memberwiseclone方法進(jìn)行復(fù)制了,而是新創(chuàng)建了一個(gè)全新的簡(jiǎn)歷。它完全是在內(nèi)部實(shí)現(xiàn)的,外部不用關(guān)心它的實(shí)現(xiàn) resume newresume = new resume( this .mworkexperience); newresume.msex = this .msex; newresume.mname = this .mname; newresume.mage = this .mage; return newresume; } } public class workexperience :icloneable { public datetime startdate { get ; set ; } public datetime enddate { get ; set ; } public string company { get ; set ; } public string position { get ; set ; } public object clone() { // 使用.net 為我們提供的淺復(fù)制對(duì)象的方法,因?yàn)檫@里已經(jīng)沒(méi)有引用對(duì)象了(string雖然是引用類(lèi)型,但.net為我們做了特別處理,可以像值類(lèi)型一樣使用它)。 return this .memberwiseclone(); } } |
測(cè)試代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[testmethod] public void testdeepcopy() { 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); } |
運(yùn)行測(cè)試,測(cè)試通過(guò),這正是我們期望的結(jié)果。
4. 模式總結(jié)
4.1 優(yōu)點(diǎn)
4.1.1 隱藏了對(duì)象的創(chuàng)建細(xì)節(jié),對(duì)有些初始化需要占用很多資源的類(lèi)來(lái)說(shuō),對(duì)性能也有很大提高。
4.1.2 在需要新對(duì)象時(shí),可以使用clone來(lái)快速創(chuàng)建創(chuàng)建一個(gè),而不用使用new來(lái)構(gòu)建。
4.2 缺點(diǎn)
4.2.1 每一個(gè)類(lèi)都需要一個(gè)clone方法,而且必須通盤(pán)考慮。對(duì)于深拷貝來(lái)說(shuō),每個(gè)關(guān)聯(lián)到的類(lèi)型都不許實(shí)現(xiàn)iclonable接口,并且每增加或修改一個(gè)字段是都需要更新clone方法。
4.3 適用場(chǎng)景
4.3.1 類(lèi)初始化需要消化非常多的資源,這個(gè)資源包括數(shù)據(jù)、硬件資源等
4.3.2 通過(guò)new產(chǎn)生一個(gè)對(duì)象需要非常繁瑣的數(shù)據(jù)準(zhǔn)備或訪(fǎng)問(wèn)權(quán)限,則可以使用原型模式
4.3.3 一個(gè)對(duì)象需要提供給其他對(duì)象訪(fǎng)問(wèn),而且各個(gè)調(diào)用者可能都需要修改其值時(shí),可以考慮使用原型模式拷貝多個(gè)對(duì)象供調(diào)用者使用。
以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。