1. 概述
通過復制一個已經存在的實例來創建一個新的實例。被復制的實例被稱為原型,這個原型是可定制的。
2. 模式中的角色
2.1 抽象原型類(abstract prototype):提供一個克隆接口
2.2 具體原型類(concrete prototype): 及實現了克隆接口的具體原型類
3. 實例:求職網站上現在都支持多份簡歷,如果每創建一份簡歷都要從頭至尾地填寫一遍,那也是非常讓人沮喪的事。其實針對我們的求職崗位的不同,不同的簡歷可能只要修改局部內容就可以了,而不用全部重新構建一份新的簡歷。復制一份簡歷,然后做局部修改是最讓人省心的了!
3.1 實現類圖
類圖解讀
在.net中,system命名空間已經為我們提供了一個icloneable接口,它包含了一個方法clone(),實現這個接口就完成了原型模式。
3.2 在寫實現代碼之前,先要理解一下深復制與淺復制。
3.2.1 淺復制:將原來對象中的所有字段逐個復制到一個新對象,如果字段是值類型,則簡單地復制一個副本到新對象,改變新對象的值類型字段不會影響原對象;如果字段是引用類型,則復制的是引用,改變目標對象中引用類型字段的值將會影響原對象。例如, 如果一個對象有一個指向引用類型(如例子中的工作經歷)的字段, 并且我們對該對象做了一個淺復制, 那麼兩個對象將引用同一個引用(即同一段工作經歷)。
3.2.2 深復制:與淺復制不同之處在于對引用類型的處理,深復制將新對象中引用類型字段指向復制過的新對象,改變新對象中引用的任何對象,不會影響到原來的對象中對應字段的內容。例如,如果一個對象有一個指向引用類型(如例子中的工作經歷)的字段,并且對該對象做了一個深復制的話.我門將創建一個新的對象(即新的工作經歷)。
3.3 簡歷的淺復制實現
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> /// 實現了icloneable接口的簡歷類 /// </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> /// 關聯了一個引用類型 /// </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> /// 實現icloneable接口的clone方法 /// </summary> /// <returns></returns> public object clone() { // .net 為我們提供的淺復制對象的方法 return this .memberwiseclone(); } } /// <summary> /// 工作經歷類 /// </summary> public class workexperience { public datetime startdate { get ; set ; } public datetime enddate { get ; set ; } public string company { get ; set ; } public string position { get ; set ; } } |
下面是測試代碼
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); } |
這里期望的是三個斷言都能運行成功,但是卻是失敗的,原因是:由于我們使用的是淺復制,所以myfirstresume, mysecondresume 和 mythirdresume引用的是同一個對象,因此最終的結果是 三個簡歷的workexperience.company都是“my third company".
3.4 簡歷的深復制實現
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> /// 實現了icloneable接口的簡歷類 /// </summary> public class resume : icloneable { public resume() { mworkexperience = new workexperience(); } /// <summary> /// 這里使用一個私有的構造函數來對其連接到的引用類型進行復制 /// </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> /// 設置功過經歷 /// </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> /// 實現icloneable接口的clone方法 /// </summary> /// <returns></returns> public object clone() { // 這里不再使用memberwiseclone方法進行復制了,而是新創建了一個全新的簡歷。它完全是在內部實現的,外部不用關心它的實現 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 為我們提供的淺復制對象的方法,因為這里已經沒有引用對象了(string雖然是引用類型,但.net為我們做了特別處理,可以像值類型一樣使用它)。 return this .memberwiseclone(); } } |
測試代碼如下
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); } |
運行測試,測試通過,這正是我們期望的結果。
4. 模式總結
4.1 優點
4.1.1 隱藏了對象的創建細節,對有些初始化需要占用很多資源的類來說,對性能也有很大提高。
4.1.2 在需要新對象時,可以使用clone來快速創建創建一個,而不用使用new來構建。
4.2 缺點
4.2.1 每一個類都需要一個clone方法,而且必須通盤考慮。對于深拷貝來說,每個關聯到的類型都不許實現iclonable接口,并且每增加或修改一個字段是都需要更新clone方法。
4.3 適用場景
4.3.1 類初始化需要消化非常多的資源,這個資源包括數據、硬件資源等
4.3.2 通過new產生一個對象需要非常繁瑣的數據準備或訪問權限,則可以使用原型模式
4.3.3 一個對象需要提供給其他對象訪問,而且各個調用者可能都需要修改其值時,可以考慮使用原型模式拷貝多個對象供調用者使用。
以上就是本文的全部內容,希望能給大家一個參考,也希望大家多多支持服務器之家。