本文實例講述了c#設計模式之builder生成器模式解決帶老婆配置電腦問題。分享給大家供大家參考,具體如下:
一、理論定義
生成器模式 又叫:建造者模式,它 可以 把一個 復雜的對象,分步驟創建。
二、應用舉例
需求描述:話說發工資了,打算去崗頂百腦匯 給老婆配置一臺電腦。
ok,坐著brt,就出發了。
到崗頂,一美女撲面而來,面帶微笑:先生,請問看中那個品牌,過來看一下嘛!
人家都開口了,盛情難卻,就看下吧。
三、具體編碼
1.cpu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// cpu /// </summary> public class cpu { /// <summary> /// cpu品牌 /// </summary> public string brand { get ; set ; } /// <summary> /// cpu系列名 /// </summary> public string serialsname { get ; set ; } //其他屬性........... } } |
2.主板
主板motherboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// 主板 /// </summary> public class motherboard { /// <summary> /// 主板品牌 /// </summary> public string brand { get ; set ; } /// <summary> /// 主板系列名 /// </summary> public string serialsname { get ; set ; } //其他屬性........... } } |
3.內存條
內存條memory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// memory /// </summary> public class memory { /// <summary> /// memory品牌 /// </summary> public string brand { get ; set ; } /// <summary> /// memory系列名 /// </summary> public string serialsname { get ; set ; } //其他屬性........... } } |
4.硬盤
硬盤harddisk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// harddisk /// </summary> public class harddisk { /// <summary> /// harddisk品牌 /// </summary> public string brand { get ; set ; } /// <summary> /// harddisk系列名 /// </summary> public string serialsname { get ; set ; } //其他屬性........... } } |
5.顯卡
顯卡graphiccard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> ///顯卡 graphiccard /// </summary> public class graphiccard { /// <summary> /// 顯卡graphiccard品牌 /// </summary> public string brand { get ; set ; } /// <summary> /// 顯卡graphiccard系列名 /// </summary> public string serialsname { get ; set ; } //其他屬性........... } } |
6.顯示器
顯示器display
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// display /// </summary> public class display { /// <summary> /// 顯示器品牌 /// </summary> public string brand { get ; set ; } /// <summary> /// 系列名 /// </summary> public string serialsname { get ; set ; } //其他屬性........... } } |
7.音箱
音箱speakers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// 音箱speakers /// </summary> public class speakers { /// <summary> /// speakers品牌 /// </summary> public string brand { get ; set ; } /// <summary> /// speakers系列名 /// </summary> public string serialsname { get ; set ; } //其他屬性........... } } |
8.鍵盤
鍵盤keyboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// 鍵盤keyboard /// </summary> public class keyboard { /// <summary> /// keyboard品牌 /// </summary> public string brand { get ; set ; } /// <summary> /// 鍵盤keyboard系列名 /// </summary> public string serialsname { get ; set ; } //其他屬性........... } } |
9.鼠標
鼠標mouse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// 鼠標mouse /// </summary> public class mouse { /// <summary> /// mouse品牌 /// </summary> public string brand { get ; set ; } /// <summary> /// 鼠標mouse系列名 /// </summary> public string serialsname { get ; set ; } //其他屬性........... } } |
10.電腦配置單config
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
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { public class config { /// <summary> /// cpu /// </summary> public cpu cpu { get ; set ; } /// <summary> /// 顯示器 /// </summary> public display display { get ; set ; } /// <summary> /// 主板 /// </summary> public motherboard motherboard { get ; set ; } /// <summary> /// 內存條 /// </summary> public memory memory { get ; set ; } /// <summary> /// 硬盤 /// </summary> public harddisk harddisk { get ; set ; } /// <summary> /// 顯卡 /// </summary> public graphiccard graphiccard { get ; set ; } /// <summary> /// 音箱 /// </summary> public speakers speakers { get ; set ; } /// <summary> /// 鍵盤 /// </summary> public keyboard keyboard { get ; set ; } /// <summary> /// 鼠標 /// </summary> public mouse mouse { get ; set ; } } } |
11.一臺電腦computer
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
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// 電腦 /// </summary> public class computer { /// <summary> /// cpu /// </summary> public cpu cpu { get ; set ; } /// <summary> /// 顯示器 /// </summary> public display display { get ; set ; } /// <summary> /// 主板 /// </summary> public motherboard motherboard { get ; set ; } /// <summary> /// 內存條 /// </summary> public memory memory { get ; set ; } /// <summary> /// 硬盤 /// </summary> public harddisk harddisk { get ; set ; } /// <summary> /// 顯卡 /// </summary> public graphiccard graphiccard { get ; set ; } /// <summary> /// 音箱 /// </summary> public speakers speakers { get ; set ; } /// <summary> /// 鍵盤 /// </summary> public keyboard keyboard { get ; set ; } /// <summary> /// 鼠標 /// </summary> public mouse mouse { get ; set ; } /// <summary> /// 電腦品牌廠商 /// </summary> public manufactures manufactures { get ; set ; } /// <summary> /// 屬于的系列 /// </summary> public string serials{ get ; set ; } /// <summary> /// 顯示電腦配置 /// </summary> public void showconfig() { console.writeline(manufactures + "\t" + serials+ " 系列的配置如下:" ); console.writeline( "--------------------------------------------------" ); console.writeline( "配件 品牌\t 系列" ); console.writeline( "cpu " + cpu.brand + "\t " + cpu.serialsname + "系列" ); console.writeline( "主板 " + motherboard.brand + "\t " + motherboard.serialsname + "系列" ); console.writeline( "內存條 " + memory.brand + "\t " + memory.serialsname + "系列" ); console.writeline( "硬盤 " + harddisk.brand + "\t " + harddisk.serialsname + "系列" ); console.writeline( "顯卡 " + graphiccard.brand + "\t " + graphiccard.serialsname + "系列" ); console.writeline( "顯示器 " + display.brand + "\t " + display.serialsname + "系列" ); console.writeline( "音箱 " + speakers.brand + "\t " + speakers.serialsname + "系列" ); console.writeline( "鍵盤 " + keyboard.brand + "\t " + keyboard.serialsname + "系列" ); console.writeline( "鼠標 " + mouse.brand + "\t " + mouse.serialsname + "系列" ); console.writeline(); console.writeline(); } } } |
12.某一品牌 某一系列 產品的 具體配置
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
|
using system; using system.collections.generic; using system.linq; using system.text; using com.design.gof.builder.factory; namespace com.design.gof.builder { /// <summary> /// 某一個產品的配置 /// </summary> public class serialsmodel { private config cfg = null ; /// <summary> /// 獲取電腦配置單 /// </summary> /// <param name="manufactures"></param> /// <param name="serialsname"></param> public serialsmodel(manufactures manufactures, string serialsname) { cfg= configfactory.getconfig(manufactures, serialsname); } /// <summary> /// 具體配置,每個產品系列都有對應的產品配置單 /// </summary> public config cfg { get { return cfg; } } } } |
13.電腦配置單,讀取的是xml文件,一共有三個測試文件,聯想thinkpad.xml,三星.xml,索尼.xml,下面只顯示聯想,其他的隨附件下載
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
|
<? xml version = "1.0" encoding = "utf-8" ?> < products > < product brand = "lenovo" serials = "聯想ideacentre k330" > < config > < cpu brand = "intel" >intel 酷睿 i5 2320 </ cpu > < motherboard brand = "華碩" >華碩 h61系列</ motherboard > < memory brand = "金士頓" >金士頓 xxxx</ memory > < harddisk brand = "希捷" >希捷1tb 7200轉,sata2</ harddisk > < graphiccard brand = "華碩" >華碩顯卡xxxx</ graphiccard > < display brand = "lenovo" >聯想顯示器xxxx</ display > < speakers brand = "lenovo" >聯想xxxx</ speakers > < keyboard brand = "lenovo" >聯想鍵盤xxxx</ keyboard > < mouse brand = "微軟" >微軟鼠標xxxx</ mouse > </ config > </ product > < product brand = "thinkpad" serials = "聯想y670p-ifi" > < config > < cpu brand = "intel" >intel 酷睿i5 2450m </ cpu > < motherboard brand = "華碩" >華碩 h61系列</ motherboard > < memory brand = "金士頓" >金士頓 xxxx</ memory > < harddisk brand = "希捷" >希捷500g xxxx</ harddisk > < graphiccard brand = "華碩" >華碩顯卡xxxx</ graphiccard > < display brand = "lg" >lg顯示器xxxx</ display > < speakers brand = "漫步者" >漫步者xxxx</ speakers > < keyboard brand = "微軟" >微軟鍵盤xxxx</ keyboard > < mouse brand = "羅技" >羅技鼠標xxxx</ mouse > </ config > </ product > < product brand = "thinkpad" serials = "聯想b470p-ifi" > < config > < cpu brand = "intel" >intel 酷睿i5 2450m </ cpu > < motherboard brand = "華碩" >華碩 h61系列</ motherboard > < memory brand = "金士頓" >金士頓 xxxx</ memory > < harddisk brand = "希捷" >希捷500g xxxx</ harddisk > < graphiccard brand = "華碩" >華碩顯卡xxxx</ graphiccard > < display brand = "lg" >lg顯示器xxxx</ display > < speakers brand = "漫步者" >漫步者xxxx</ speakers > < keyboard brand = "微軟" >微軟鍵盤xxxx</ keyboard > < mouse brand = "長城" >長城鼠標xxxx</ mouse > </ config > </ product > </ products > |
14.一個專門負責獲取電腦 配置單的 簡單工具類
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
|
using system; using system.collections.generic; using system.linq; using system.xml.linq; using system.xml.xpath; using system.text; using com.design.gof.builder; using system.xml; using system.io; namespace com.design.gof.builder.factory { public class configfactory { /// <summary> /// 獲取電腦配置單 /// </summary> /// <param name="manufactures">電腦廠商</param> /// <param name="serialname">指定系列</param> /// <returns></returns> public static config getconfig(manufactures manufactures, string serialname) { config cfg = new config(); //從xml文件,加載電腦配置單 xdocument doc = xdocument.parse(file.readalltext(appdomain.currentdomain.basedirectory + @"\builder\data\" + manufactures + ".xml")); xpathnavigator nav=doc.createnavigator(); xpathnavigator n=nav.selectsinglenode( "/products/product[@serials='" + serialname.trim() + "']" ); n.movetochild( "config" , "" ); if (n == null ) { return cfg; } string brand = string .empty, serials = string .empty; //cpu n.movetochild( "cpu" , "" ); brand = n.getattribute( "brand" , "" ); serials = n.value; cfg.cpu = new cpu { brand = brand, serialsname = serials }; //主板motherboard n.movetonext( "motherboard" , "" ); brand = n.getattribute( "brand" , "" ); serials = n.value; cfg.motherboard = new motherboard { brand = brand, serialsname = serials }; //內存memory n.movetonext( "memory" , "" ); brand = n.getattribute( "brand" , "" ); serials = n.value; cfg.memory = new memory { brand = brand, serialsname = serials }; //硬盤harddisk n.movetonext( "harddisk" , "" ); brand = n.getattribute( "brand" , "" ); serials = n.value; cfg.harddisk = new harddisk { brand = brand, serialsname = serials }; //顯卡graphiccard n.movetonext( "graphiccard" , "" ); brand = n.getattribute( "brand" , "" ); serials = n.value; cfg.graphiccard = new graphiccard { brand = brand, serialsname = serials }; //顯示器display n.movetonext( "display" , "" ); brand = n.getattribute( "brand" , "" ); serials = n.value; cfg.display = new display { brand = brand, serialsname = serials }; //音箱speakers n.movetonext( "motherboard" , "" ); brand = n.getattribute( "brand" , "" ); serials = n.value; cfg.speakers = new speakers { brand = brand, serialsname = serials }; //鍵盤keyboard n.movetonext( "keyboard" , "" ); brand = n.getattribute( "brand" , "" ); serials = n.value; cfg.keyboard = new keyboard { brand = brand, serialsname = serials }; //鼠標motherboard n.movetonext( "mouse" , "" ); brand = n.getattribute( "brand" , "" ); serials = n.value; cfg.mouse = new mouse { brand = brand, serialsname = serials }; return cfg; } } } |
15.一個接口,定義了如何構建一個電腦
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
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { public interface ibuilder { /// <summary> /// cpu /// </summary> void buildercpu(cpu cpu); /// <summary> /// 主板 /// </summary> void buildermotherboard(motherboard motherboard); /// <summary> /// 顯示器 /// </summary> void builderdisplay(display display); /// <summary> /// 內存條 /// </summary> void buildermemory(memory memory); /// <summary> /// 硬盤 /// </summary> void builderharddisk(harddisk harddisk); /// <summary> /// 顯卡 /// </summary> void buildergraphiccard(graphiccard graphiccard); /// <summary> /// 音箱 /// </summary> void builderspeakers(speakers speakers); /// <summary> /// 鍵盤 /// </summary> void builderkeyboard(keyboard keyboard); /// <summary> /// 鼠標 /// </summary> void buildermouse(mouse mouse); /// <summary> /// 獲取組裝好的電腦 /// </summary> /// <returns></returns> computer getcomputer(); } } |
16.聯想電腦
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// thinkpad品牌廠商 /// </summary> public class thinkpad:ibuilder { computer computer = null ; //電腦 serialsmodel serials = null ; //某個產品系列的具體產品 public thinkpad(manufactures manufactures, string serialsname) { computer = new computer { manufactures = manufactures, serials = serialsname }; serials = new serialsmodel(manufactures, serialsname); } #region 裝配電腦 /// <summary> /// 1.組裝 cpu /// </summary> /// <param name="cpu"></param> public void buildercpu(cpu cpu) { computer.cpu = cpu; } /// <summary> /// 2.組裝 主板 /// </summary> /// <param name="motherboard"></param> public void buildermotherboard(motherboard motherboard) { computer.motherboard = motherboard; } /// <summary> /// 3.組裝 內存條 /// </summary> /// <param name="display"></param> public void buildermemory(memory memory) { computer.memory = memory; } /// <summary> ///4.組裝 硬盤 /// </summary> /// <param name="display"></param> public void builderharddisk(harddisk harddisk) { computer.harddisk = harddisk; } /// <summary> /// 5.組裝 顯卡 /// </summary> /// <param name="display"></param> public void buildergraphiccard(graphiccard graphiccard) { computer.graphiccard = graphiccard; } /// <summary> /// 6.組裝 顯示器 /// </summary> /// <param name="display"></param> public void builderdisplay(display display) { computer.display = display; } /// <summary> /// 7.組裝 音箱 /// </summary> /// <param name="display"></param> public void builderspeakers(speakers speakers) { computer.speakers = speakers; } /// <summary> /// 8.組裝 鍵盤 /// </summary> /// <param name="display"></param> public void builderkeyboard(keyboard keyboard) { computer.keyboard = keyboard; } /// <summary> /// 9.組裝 鼠標 /// </summary> /// <param name="display"></param> public void buildermouse(mouse mouse) { computer.mouse = mouse; } #endregion /// <summary> /// 獲取組裝后的電腦 /// </summary> /// <returns></returns> public computer getcomputer() { //步驟1--cpu buildercpu(serials.cfg.cpu); //步驟2---主板 buildermotherboard(serials.cfg.motherboard); //步驟3--內存條 buildermemory(serials.cfg.memory); //步驟4--硬盤 builderharddisk(serials.cfg.harddisk); //步驟5--顯卡 buildergraphiccard(serials.cfg.graphiccard); //步驟6--顯示器 builderdisplay(serials.cfg.display); //步驟7--音箱 builderspeakers(serials.cfg.speakers); //步驟8--鍵盤 builderkeyboard(serials.cfg.keyboard); //步驟9--鼠標 buildermouse(serials.cfg.mouse); return computer; } } } |
17.索尼電腦
sony
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// sony索尼品牌廠商 /// </summary> public class sony:ibuilder { computer computer = null ; //電腦 serialsmodel serials = null ; //某個產品系列的具體產品 public sony(manufactures manufactures, string serialsname) { computer = new computer { manufactures = manufactures, serials = serialsname }; serials = new serialsmodel(manufactures, serialsname); } #region 裝配電腦 /// <summary> /// 1.組裝 cpu /// </summary> /// <param name="cpu"></param> public void buildercpu(cpu cpu) { computer.cpu = cpu; } /// <summary> /// 2.組裝 主板 /// </summary> /// <param name="motherboard"></param> public void buildermotherboard(motherboard motherboard) { computer.motherboard = motherboard; } /// <summary> /// 3.組裝 內存條 /// </summary> /// <param name="display"></param> public void buildermemory(memory memory) { computer.memory = memory; } /// <summary> ///4.組裝 硬盤 /// </summary> /// <param name="display"></param> public void builderharddisk(harddisk harddisk) { computer.harddisk = harddisk; } /// <summary> /// 5.組裝 顯卡 /// </summary> /// <param name="display"></param> public void buildergraphiccard(graphiccard graphiccard) { computer.graphiccard = graphiccard; } /// <summary> /// 6.組裝 顯示器 /// </summary> /// <param name="display"></param> public void builderdisplay(display display) { computer.display = display; } /// <summary> /// 7.組裝 音箱 /// </summary> /// <param name="display"></param> public void builderspeakers(speakers speakers) { computer.speakers = speakers; } /// <summary> /// 8.組裝 鍵盤 /// </summary> /// <param name="display"></param> public void builderkeyboard(keyboard keyboard) { computer.keyboard = keyboard; } /// <summary> /// 9.組裝 鼠標 /// </summary> /// <param name="display"></param> public void buildermouse(mouse mouse) { computer.mouse = mouse; } #endregion /// <summary> /// 獲取組裝后的電腦 /// </summary> /// <returns></returns> public computer getcomputer() { //步驟1--cpu buildercpu(serials.cfg.cpu); //步驟2---主板 buildermotherboard(serials.cfg.motherboard); //步驟3--內存條 buildermemory(serials.cfg.memory); //步驟4--硬盤 builderharddisk(serials.cfg.harddisk); //步驟5--顯卡 buildergraphiccard(serials.cfg.graphiccard); //步驟6--顯示器 builderdisplay(serials.cfg.display); //步驟7--音箱 builderspeakers(serials.cfg.speakers); //步驟8--鍵盤 builderkeyboard(serials.cfg.keyboard); //步驟9--鼠標 buildermouse(serials.cfg.mouse); return computer; } } } |
18.三星電腦
三星
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// samsung索尼品牌廠商 /// </summary> public class samsung:ibuilder { computer computer = null ; //電腦 serialsmodel serials = null ; //某個產品系列的具體產品 public samsung(manufactures manufactures, string serialsname) { computer = new computer { manufactures = manufactures, serials = serialsname }; serials = new serialsmodel(manufactures, serialsname); } #region 裝配電腦 /// <summary> /// 1.組裝 cpu /// </summary> /// <param name="cpu"></param> public void buildercpu(cpu cpu) { computer.cpu = cpu; } /// <summary> /// 2.組裝 主板 /// </summary> /// <param name="motherboard"></param> public void buildermotherboard(motherboard motherboard) { computer.motherboard = motherboard; } /// <summary> /// 3.組裝 內存條 /// </summary> /// <param name="display"></param> public void buildermemory(memory memory) { computer.memory = memory; } /// <summary> ///4.組裝 硬盤 /// </summary> /// <param name="display"></param> public void builderharddisk(harddisk harddisk) { computer.harddisk = harddisk; } /// <summary> /// 5.組裝 顯卡 /// </summary> /// <param name="display"></param> public void buildergraphiccard(graphiccard graphiccard) { computer.graphiccard = graphiccard; } /// <summary> /// 6.組裝 顯示器 /// </summary> /// <param name="display"></param> public void builderdisplay(display display) { computer.display = display; } /// <summary> /// 7.組裝 音箱 /// </summary> /// <param name="display"></param> public void builderspeakers(speakers speakers) { computer.speakers = speakers; } /// <summary> /// 8.組裝 鍵盤 /// </summary> /// <param name="display"></param> public void builderkeyboard(keyboard keyboard) { computer.keyboard = keyboard; } /// <summary> /// 9.組裝 鼠標 /// </summary> /// <param name="display"></param> public void buildermouse(mouse mouse) { computer.mouse = mouse; } #endregion /// <summary> /// 獲取組裝后的電腦 /// </summary> /// <returns></returns> public computer getcomputer() { //步驟1--cpu buildercpu(serials.cfg.cpu); //步驟2---主板 buildermotherboard(serials.cfg.motherboard); //步驟3--內存條 buildermemory(serials.cfg.memory); //步驟4--硬盤 builderharddisk(serials.cfg.harddisk); //步驟5--顯卡 buildergraphiccard(serials.cfg.graphiccard); //步驟6--顯示器 builderdisplay(serials.cfg.display); //步驟7--音箱 builderspeakers(serials.cfg.speakers); //步驟8--鍵盤 builderkeyboard(serials.cfg.keyboard); //步驟9--鼠標 buildermouse(serials.cfg.mouse); return computer; } } } |
19.美女銷售員,指導我們買電腦
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
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.builder { /// <summary> /// 電腦廠家,銷售員指導 消費者 要購買那個 品牌 的電腦 /// </summary> public enum manufactures { acer宏碁=1, alienware=2,長城=3, 戴爾=4, 東芝=5, 典籍=6, 多彩=7, dcmofa=8,eser宇朔=9,富士通=10, 方正=11,gateway華碩=12, 惠普=13, 海爾=14, 瀚斯寶麗=15,intel=16, 技嘉=17,聯想=18, 聯想thinkpad=19, 雷蛇=20, lg=21, 鐳波=22, msi微星=23, 明唐=24,nec=25, 蘋果=26,清華=27,同方=28, 七喜=29, 七彩虹=30,神舟=31, 索尼=32, 三星=33, 松下=34, 史密斯=35, 索泰=36, 神酷=37, terransforce=38,微軟=39, 萬利達=40, 新藍=41,優派=42 } public class salesman { /// <summary> /// 電腦配置 /// </summary> public void showconfig() { builder.getcomputer().showconfig(); } /// <summary> /// 制造商 /// </summary> public ibuilder builder { get ; set ; } } } |
20.主函數調用
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
|
using system; using system.collections.generic; using system.linq; using system.text; using com.design.gof.builder; namespace com.design.gof.test { class program { static void main( string [] args) { //這位先生,請問你看中哪個型號?這邊來看下吧! salesman salesman = new salesman(); //美女介紹的 第一臺電腦 salesman.builder = new thinkpad(manufactures.聯想thinkpad, "聯想ideacentre k330" ); salesman.showconfig(); //電腦配置 //第二臺電腦 salesman.builder = new sony(manufactures.索尼, "索尼e14a17ecw" ); salesman.showconfig(); //電腦配置 //第三臺電腦 salesman.builder = new samsung(manufactures.三星, "三星300e4a-s0g" ); salesman.showconfig(); //電腦配置 console.readkey(); } } } |
21.運行結果
22.總結
本來想把每個電腦配件再細化,發現工程很大,電腦 產品的屬性很多,所以只列出2個字段。
附件里面包括了程序源碼。也包括其他項目的測試,有控制臺,有web。
附:完整實例代碼點擊此處本站下載。
希望本文所述對大家c#程序設計有所幫助。
原文鏈接:http://www.cnblogs.com/HCCZX/archive/2012/08/08/2628452.html