本文实例讲述了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