您好,登錄后才能下訂單哦!
本文實例講述了C#設計模式之Builder生成器模式解決帶老婆配置電腦問題。分享給大家供大家參考,具體如下:
一、理論定義
生成器模式 又叫:建造者模式,它 可以 把一個 復雜的對象,分步驟創(chuàng)建。
二、應用舉例
需求描述:話說發(fā)工資了,打算去崗頂百腦匯 給老婆配置一臺電腦。
OK,坐著BRT,就出發(fā)了。
到崗頂,一美女撲面而來,面帶微笑:先生,請問看中那個品牌,過來看一下嘛!
人家都開口了,盛情難卻,就看下吧。
三、具體編碼
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,優(yōu)派=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.總結
本來想把每個電腦配件再細化,發(fā)現工程很大,電腦 產品的屬性很多,所以只列出2個字段。
附件里面包括了程序源碼。也包括其他項目的測試,有控制臺,有web。
附:完整實例代碼點擊此處本站下載。
更多關于C#相關內容還可查看本站專題:《C#數據結構與算法教程》、《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數組操作技巧總結》及《C#面向對象程序設計入門教程》
希望本文所述對大家C#程序設計有所幫助。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。