C#中Builder生成器模式解决配置电脑的问题详解

C#中Builder生成器模式解决配置电脑的问题详解,第1张

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

本文实例讲述了C#设计模式之Builder生成器模式解决带老婆配置电脑问题。分享给大家供大家参考,具体如下:

一、理论定义

生成器模式 又叫:建造者模式,它 可以 把一个 复杂的对象,分步骤创建。

二、应用举例

需求描述:话说发工资了,打算去岗顶百脑汇 给老婆配置一台电脑。
OK,坐着BRT,就出发了。
到岗顶,一美女扑面而来,面带微笑:先生,请问看中那个品牌,过来看一下嘛!
人家都开口了,盛情难却,就看下吧。

三、具体编码

1.cpu


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


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


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


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


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


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


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


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


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


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


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.某一品牌 某一系列 产品的 具体配置


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,下面只显示联想,其他的随附件下载


<?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.一个专门负责获取电脑 配置单的 简单工具类


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.一个接口,定义了如何构建一个电脑


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.联想电脑


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


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.三星电脑

三星


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.美女销售员,指导我们买电脑


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.主函数调用


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#中Builder生成器模式解决配置电脑的问题详解全部内容,希望文章能够帮你解决C#中Builder生成器模式解决配置电脑的问题详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1210894.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存