一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
主站蜘蛛池模板: 久久精品国产色蜜蜜麻豆国语版 | 动漫美女人物被黄漫在线看 | 亚洲精品国产自在现线最新 | 成人永久免费福利视频网站 | 久久久96 | 日韩福利网站 | 亚洲激情视频在线 | 我的好妈妈7中字在线观看韩国 | 亚洲国产剧情中文视频在线 | 精品亚洲欧美中文字幕在线看 | 日本无翼乌漫画 | 国产精品亚洲w码日韩中文 国产精品香蕉在线观看不卡 | 国产伦精一区二区三区视频 | 动漫美女强行被吸乳做羞羞事 | 男女拍拍拍免费视频网站 | 草莓绿巨人香蕉茄子芭乐 | 久久伊人影视 | 日本精品中文字幕在线播放 | 情趣内衣在线观看 | 国产精品性视频免费播放 | 日本免费观看的视频在线 | 成年看片免费高清观看 | 亚洲精品短视频 | 亚洲国产精品综合久久一线 | 丝袜高跟小说 | 亚洲精品乱码久久久久久蜜桃欧美 | 亚洲国产欧美另类 | 亚州笫一色惰网站 | 欧美精品久久久久久久影视 | 国产在线视频第一页 | 国产精品29页 | 免费真实播放国产乱子伦 | 国产国语videosex另类 | 叛佛 作者满栀小说免费阅读 | 91一个人的在线观看www | 亚洲精品青青草原avav久久qv | 男人操女生 | 国产梦呦精品 | 国产精品香蕉 | 国产一级一级一级成人毛片 | 日本性生活免费看 |