我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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

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

成都创新互联公司专注于伽师企业网站建设,成都响应式网站建设公司,成都商城网站开发。伽师网站建设公司,为伽师等地区提供建站服务。全流程按需网站策划,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务

一、理论定义

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

二、应用举例

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

三、具体编码

1.CPU

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Builder
{
  /// 
  /// CPU
  /// 
  public class CPU
  {
    /// 
    /// CPU品牌
    /// 
    public string Brand { get; set; }
    /// 
    /// CPU系列名
    /// 
    public string SerialsName { get; set; }
    //其他属性...........
  }
}

2.主板

主板MotherBoard

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Builder
{
  /// 
  /// 主板
  /// 
  public class MotherBoard
  {
    /// 
    /// 主板品牌
    /// 
    public string Brand { get; set; }
    /// 
    /// 主板系列名
    /// 
    public string SerialsName { get; set; }
    //其他属性...........
  }
}

3.内存条

内存条Memory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Builder
{
  /// 
  /// Memory
  /// 
  public class Memory
  {
    /// 
    /// Memory品牌
    /// 
    public string Brand { get; set; }
    /// 
    /// Memory系列名
    /// 
    public string SerialsName { get; set; }
    //其他属性...........
  }
}

4.硬盘

硬盘HardDisk

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Builder
{
  /// 
  /// HardDisk
  /// 
  public class HardDisk
  {
    /// 
    /// HardDisk品牌
    /// 
    public string Brand { get; set; }
    /// 
    /// HardDisk系列名
    /// 
    public string SerialsName { get; set; }
    //其他属性...........
  }
}

5.显卡

显卡GraphicCard

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Builder
{
  /// 
  ///显卡 GraphicCard
  /// 
  public class GraphicCard
  {
    /// 
    /// 显卡GraphicCard品牌
    /// 
    public string Brand { get; set; }
    /// 
    /// 显卡GraphicCard系列名
    /// 
    public string SerialsName { get; set; }
    //其他属性...........
  }
}

6.显示器

显示器Display

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Builder
{
  /// 
  /// Display
  /// 
  public class Display
  {
    /// 
    /// 显示器品牌
    /// 
    public string Brand { get; set; }
   /// 
   /// 系列名
   /// 
   public string SerialsName { get; set; }
    //其他属性...........
  }
}

7.音箱

音箱Speakers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Builder
{
  /// 
  /// 音箱Speakers
  /// 
  public class Speakers
  {
    /// 
    /// Speakers品牌
    /// 
    public string Brand { get; set; }
    /// 
    /// Speakers系列名
    /// 
    public string SerialsName { get; set; }
    //其他属性...........
  }
}

8.键盘

键盘Keyboard

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Builder
{
  /// 
  /// 键盘Keyboard
  /// 
  public class Keyboard
  {
    /// 
    /// Keyboard品牌
    /// 
    public string Brand { get; set; }
    /// 
    /// 键盘Keyboard系列名
    /// 
    public string SerialsName { get; set; }
    //其他属性...........
  }
}

9.鼠标

鼠标Mouse

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Builder
{
  /// 
  /// 鼠标Mouse
  /// 
  public class Mouse
  {
    /// 
    /// Mouse品牌
    /// 
    public string Brand { get; set; }
    /// 
    /// 鼠标Mouse系列名
    /// 
    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
  {
    /// 
    /// CPU
    /// 
    public CPU CPU { get; set; }
    /// 
    /// 显示器
    /// 
    public Display Display { get; set; }
    /// 
    /// 主板
    /// 
    public MotherBoard MotherBoard { get; set; }
    /// 
    /// 内存条
    /// 
    public Memory Memory { get; set; }
    /// 
    /// 硬盘
    /// 
    public HardDisk HardDisk { get; set; }
    /// 
    /// 显卡
    /// 
    public GraphicCard GraphicCard { get; set; }
    /// 
    /// 音箱
    /// 
    public Speakers Speakers { get; set; }
    /// 
    /// 键盘
    /// 
    public Keyboard Keyboard { get; set; }
    /// 
    /// 鼠标
    /// 
    public Mouse Mouse { get; set; }
  }
}

11.一台电脑Computer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Builder
{
  /// 
  /// 电脑
  /// 
  public class Computer
  {
    /// 
    /// CPU
    /// 
    public CPU CPU { get; set; }
    /// 
    /// 显示器
    /// 
    public Display Display { get; set; }
     /// 
    /// 主板
    /// 
    public MotherBoard MotherBoard { get; set; }
    /// 
    /// 内存条
    /// 
    public Memory Memory { get; set; }
    /// 
    /// 硬盘
    /// 
    public HardDisk HardDisk { get; set; }
    /// 
    /// 显卡
    /// 
    public GraphicCard GraphicCard { get; set; }
    /// 
    /// 音箱
    /// 
    public Speakers Speakers { get; set; }
    /// 
    /// 键盘
    /// 
    public Keyboard Keyboard { get; set; }
    /// 
    /// 鼠标
    /// 
    public Mouse Mouse { get; set; }
    /// 
    /// 电脑品牌厂商
    /// 
    public Manufactures Manufactures { get; set; }
    /// 
    /// 属于的系列
    /// 
    public string Serials{ get; set; }
    /// 
    /// 显示电脑配置
    /// 
    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
{
  /// 
  /// 某一个产品的配置
  /// 
  public class SerialsModel
  {
    private Config cfg = null;
    /// 
    /// 获取电脑配置单
    /// 
    /// 
    /// 
    public SerialsModel(Manufactures Manufactures, string SerialsName)
    {
      cfg= ConfigFactory.GetConfig(Manufactures, SerialsName);
    }
    /// 
    /// 具体配置,每个产品系列都有对应的产品配置单
    /// 
    public Config Cfg
    {
      get { return cfg; }
    }
  }
}

13.电脑配置单,读取的是XML文件,一共有三个测试文件,联想ThinkPad.xml,三星.xml,索尼.xml,下面只显示联想,其他的随附件下载

<?xml version="1.0" encoding="utf-8" ?>

 
  
   Intel 酷睿 i5 2320 
   华硕 H61系列
   金士顿 XXXX
   希捷1TB 7200转,SATA2
   华硕显卡XXXX
   联想显示器XXXX
   联想XXXX
   联想键盘XXXX
   微软鼠标XXXX
  
 
 
  
   Intel 酷睿i5 2450M 
   华硕 H61系列
   金士顿 XXXX
   希捷500G XXXX
   华硕显卡XXXX
   LG显示器XXXX
   漫步者XXXX
   微软键盘XXXX
   罗技鼠标XXXX
  
 
 
  
   Intel 酷睿i5 2450M 
   华硕 H61系列
   金士顿 XXXX
   希捷500G XXXX
   华硕显卡XXXX
   LG显示器XXXX
   漫步者XXXX
   微软键盘XXXX
   长城鼠标XXXX
  
 


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
  {
    /// 
    /// 获取电脑配置单
    /// 
    /// 电脑厂商
    /// 指定系列
    /// 
    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
  {
    /// 
    /// CPU
    /// 
    void BuilderCPU(CPU cpu);
    /// 
    /// 主板
    /// 
    void BuilderMotherBoard(MotherBoard motherBoard);
    /// 
    /// 显示器
    /// 
    void BuilderDisplay(Display display);
    /// 
    /// 内存条
    /// 
    void BuilderMemory(Memory memory);
    /// 
    /// 硬盘
    /// 
    void BuilderHardDisk(HardDisk hardDisk);
    /// 
    /// 显卡
    /// 
    void BuilderGraphicCard(GraphicCard graphicCard);
    /// 
    /// 音箱
    /// 
    void BuilderSpeakers(Speakers speakers);
    /// 
    /// 键盘
    /// 
    void BuilderKeyboard(Keyboard keyboard);
    /// 
    /// 鼠标
    /// 
    void BuilderMouse(Mouse mouse);
    /// 
    /// 获取组装好的电脑
    /// 
    /// 
    Computer GetComputer();
  }
}

16.联想电脑

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Builder
{
  /// 
  /// ThinkPad品牌厂商
  /// 
  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 装配电脑
    /// 
    /// 1.组装 CPU
    /// 
    /// 
    public void BuilderCPU(CPU cpu)
    {
      computer.CPU = cpu;
    }
    /// 
    /// 2.组装 主板
    /// 
    /// 
    public void BuilderMotherBoard(MotherBoard motherBoard)
    {
      computer.MotherBoard = motherBoard;
    }
    /// 
    /// 3.组装 内存条
    /// 
    /// 
    public void BuilderMemory(Memory memory)
    {
      computer.Memory = memory;
    }
    /// 
    ///4.组装 硬盘
    /// 
    /// 
    public void BuilderHardDisk(HardDisk hardDisk)
    {
      computer.HardDisk = hardDisk;
    }
    /// 
    /// 5.组装 显卡
    /// 
    /// 
    public void BuilderGraphicCard(GraphicCard graphicCard)
    {
      computer.GraphicCard = graphicCard;
    }
    /// 
    /// 6.组装 显示器
    /// 
    /// 
    public void BuilderDisplay(Display display)
    {
      computer.Display = display;
    }
    /// 
    /// 7.组装 音箱
    /// 
    /// 
    public void BuilderSpeakers(Speakers speakers)
    {
      computer.Speakers = speakers;
    }
    /// 
    /// 8.组装 键盘
    /// 
    /// 
    public void BuilderKeyboard(Keyboard keyboard)
    {
      computer.Keyboard = keyboard;
    }
    /// 
    /// 9.组装 鼠标
    /// 
    /// 
    public void BuilderMouse(Mouse mouse)
    {
      computer.Mouse = mouse;
    }
    #endregion
    /// 
    /// 获取组装后的电脑
    /// 
    /// 
    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
{
  /// 
  /// Sony索尼品牌厂商
  /// 
  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 装配电脑
    /// 
    /// 1.组装 CPU
    /// 
    /// 
    public void BuilderCPU(CPU cpu)
    {
      computer.CPU = cpu;
    }
    /// 
    /// 2.组装 主板
    /// 
    /// 
    public void BuilderMotherBoard(MotherBoard motherBoard)
    {
      computer.MotherBoard = motherBoard;
    }
    /// 
    /// 3.组装 内存条
    /// 
    /// 
    public void BuilderMemory(Memory memory)
    {
      computer.Memory = memory;
    }
    /// 
    ///4.组装 硬盘
    /// 
    /// 
    public void BuilderHardDisk(HardDisk hardDisk)
    {
      computer.HardDisk = hardDisk;
    }
    /// 
    /// 5.组装 显卡
    /// 
    /// 
    public void BuilderGraphicCard(GraphicCard graphicCard)
    {
      computer.GraphicCard = graphicCard;
    }
    /// 
    /// 6.组装 显示器
    /// 
    /// 
    public void BuilderDisplay(Display display)
    {
      computer.Display = display;
    }
    /// 
    /// 7.组装 音箱
    /// 
    /// 
    public void BuilderSpeakers(Speakers speakers)
    {
      computer.Speakers = speakers;
    }
    /// 
    /// 8.组装 键盘
    /// 
    /// 
    public void BuilderKeyboard(Keyboard keyboard)
    {
      computer.Keyboard = keyboard;
    }
    /// 
    /// 9.组装 鼠标
    /// 
    /// 
    public void BuilderMouse(Mouse mouse)
    {
      computer.Mouse = mouse;
    }
    #endregion
    /// 
    /// 获取组装后的电脑
    /// 
    /// 
    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
{
  /// 
  /// Samsung索尼品牌厂商
  /// 
  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 装配电脑
    /// 
    /// 1.组装 CPU
    /// 
    /// 
    public void BuilderCPU(CPU cpu)
    {
      computer.CPU = cpu;
    }
    /// 
    /// 2.组装 主板
    /// 
    /// 
    public void BuilderMotherBoard(MotherBoard motherBoard)
    {
      computer.MotherBoard = motherBoard;
    }
    /// 
    /// 3.组装 内存条
    /// 
    /// 
    public void BuilderMemory(Memory memory)
    {
      computer.Memory = memory;
    }
    /// 
    ///4.组装 硬盘
    /// 
    /// 
    public void BuilderHardDisk(HardDisk hardDisk)
    {
      computer.HardDisk = hardDisk;
    }
    /// 
    /// 5.组装 显卡
    /// 
    /// 
    public void BuilderGraphicCard(GraphicCard graphicCard)
    {
      computer.GraphicCard = graphicCard;
    }
    /// 
    /// 6.组装 显示器
    /// 
    /// 
    public void BuilderDisplay(Display display)
    {
      computer.Display = display;
    }
    /// 
    /// 7.组装 音箱
    /// 
    /// 
    public void BuilderSpeakers(Speakers speakers)
    {
      computer.Speakers = speakers;
    }
    /// 
    /// 8.组装 键盘
    /// 
    /// 
    public void BuilderKeyboard(Keyboard keyboard)
    {
      computer.Keyboard = keyboard;
    }
    /// 
    /// 9.组装 鼠标
    /// 
    /// 
    public void BuilderMouse(Mouse mouse)
    {
      computer.Mouse = mouse;
    }
    #endregion
    /// 
    /// 获取组装后的电脑
    /// 
    /// 
    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
{
  /// 
  /// 电脑厂家,销售员指导 消费者 要购买那个 品牌 的电脑
  /// 
  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
  {
    /// 
    /// 电脑配置
    /// 
    public void ShowConfig()
    {
      Builder.GetComputer().ShowConfig();
    }
    /// 
    /// 制造商
    /// 
    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.运行结果

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

22.总结

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

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

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

更多关于C#相关内容还可查看本站专题:《C#数据结构与算法教程》、《C#窗体操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》

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


当前文章:C#设计模式之Builder生成器模式解决带老婆配置电脑问题实例
转载源于:http://mswzjz.cn/article/gcsosp.html

其他资讯