服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#设计模式之Builder生成器模式解决带老婆配置电脑问题实例

C#设计模式之Builder生成器模式解决带老婆配置电脑问题实例

2022-01-21 14:13GhostRider C#

这篇文章主要介绍了C#设计模式之Builder生成器模式解决带老婆配置电脑问题,简单介绍了生成器模式的概念、功能并结合具体实例形式分析了C#生成器模式解决配电脑问题的步骤与相关操作技巧,需要的朋友可以参考下

本文实例讲述了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.运行结果

C#设计模式之Builder生成器模式解决带老婆配置电脑问题实例

22.总结

本来想把每个电脑配件再细化,发现工程很大,电脑 产品的属性很多,所以只列出2个字段。

附件里面包括了程序源码。也包括其他项目的测试,有控制台,有web。

附:完整实例代码点击此处本站下载

希望本文所述对大家c#程序设计有所帮助。

原文链接:http://www.cnblogs.com/HCCZX/archive/2012/08/08/2628452.html

延伸 · 阅读

精彩推荐
  • C#如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴...

    bbird201811792022-03-05
  • C#三十分钟快速掌握C# 6.0知识点

    三十分钟快速掌握C# 6.0知识点

    这篇文章主要介绍了C# 6.0的相关知识点,文中介绍的非常详细,通过这篇文字可以让大家在三十分钟内快速的掌握C# 6.0,需要的朋友可以参考借鉴,下面来...

    雨夜潇湘8272021-12-28
  • C#深入理解C#的数组

    深入理解C#的数组

    本篇文章主要介绍了C#的数组,数组是一种数据结构,详细的介绍了数组的声明和访问等,有兴趣的可以了解一下。...

    佳园9492021-12-10
  • C#利用C#实现网络爬虫

    利用C#实现网络爬虫

    这篇文章主要介绍了利用C#实现网络爬虫,完整的介绍了C#实现网络爬虫详细过程,感兴趣的小伙伴们可以参考一下...

    C#教程网11852021-11-16
  • C#VS2012 程序打包部署图文详解

    VS2012 程序打包部署图文详解

    VS2012虽然没有集成打包工具,但它为我们提供了下载的端口,需要我们手动安装一个插件InstallShield。网上有很多第三方的打包工具,但为什么偏要使用微软...

    张信秀7712021-12-15
  • C#C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    这篇文章主要介绍了C#设计模式之Strategy策略模式解决007大破密码危机问题,简单描述了策略模式的定义并结合加密解密算法实例分析了C#策略模式的具体使用...

    GhostRider10972022-01-21
  • C#SQLite在C#中的安装与操作技巧

    SQLite在C#中的安装与操作技巧

    SQLite,是一款轻型的数据库,用于本地的数据储存。其优点有很多,下面通过本文给大家介绍SQLite在C#中的安装与操作技巧,感兴趣的的朋友参考下吧...

    蓝曈魅11162022-01-20
  • C#C#微信公众号与订阅号接口开发示例代码

    C#微信公众号与订阅号接口开发示例代码

    这篇文章主要介绍了C#微信公众号与订阅号接口开发示例代码,结合实例形式简单分析了C#针对微信接口的调用与处理技巧,需要的朋友可以参考下...

    smartsmile20127762021-11-25