>백엔드 개발 >C#.Net 튜토리얼 >컴퓨터 구성 문제를 해결하기 위한 C#의 Builder 생성기 모드에 대한 자세한 설명

컴퓨터 구성 문제를 해결하기 위한 C#의 Builder 생성기 모드에 대한 자세한 설명

黄舟
黄舟원래의
2017-09-15 11:25:311597검색

이 글에서는 아내와 컴퓨터를 구성할 때의 문제를 해결하기 위해 C# 디자인 패턴 중 빌더 패턴을 주로 소개합니다. 빌더 패턴의 개념과 기능을 간략하게 소개하고 구체적인 예와 결합하여 C#의 단계와 단계를 분석합니다. 컴퓨터 구성 문제를 해결하기 위한 빌더 패턴. 관련 조작 기술이 필요한 친구는 참고할 수 있습니다.

이 기사에서는 컴퓨터 구성 문제를 해결하기 위해 C# 디자인 패턴의 빌더 생성기 모드를 설명하는 예제를 사용합니다. 부인. 참조용으로 모든 사람과 공유하세요. 세부 사항은 다음과 같습니다.

1. 이론적 정의

생성기 모드는 빌더 모드라고도 하며 복잡한 개체를 단계별로 생성할 수 있습니다.

2. 적용 예시

요구 사항 설명 : 급여 얘기가 나와서 강딩 바이나오휘에 가서 아내를 위한 컴퓨터를 구성할 예정입니다.
자, BRT를 타고 출발하세요.
글을 올리자 한 아름다운 여성이 미소를 지으며 다가왔습니다. 선생님, 어떤 브랜드를 좋아하시나요?
다들 얘기가 나오던데, 환대를 거부하기 힘들으니 한 번 살펴보겠습니다.

3. 특정 인코딩

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

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

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 .하드 디스크

하드 디스크


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. 그래픽 카드

그래픽 카드


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.Speaker ers

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

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

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.

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. 컴퓨터 구성 시트에는 총 3개의 테스트 파일이 있습니다. Lenovo ThinkPad.xml, Samsung.xml, Sony만 아래에 표시됩니다. 첨부 파일과 함께 다운로드할 수 있습니다

<?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=&#39;" + SerialName.Trim() + "&#39;]");
      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.Lenovo Computer


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 Computer


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.Samsung Computer

Samsung

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개의 필드만 나열됩니다.

첨부파일에는 프로그램 소스코드가 포함되어 있습니다. 또한 콘솔 및 웹을 포함한 다른 프로젝트에 대한 테스트도 포함됩니다.

위 내용은 컴퓨터 구성 문제를 해결하기 위한 C#의 Builder 생성기 모드에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.