设计模式系列:构建者模式
using System; using System.Collections.Generic; using System.Linq; using System.Text; ///在电脑组装机总有这样的经历,我们到了店里,先会有一个销售人员来询问你希望装的机器是怎么样的 ///的配置,他会给你一些建议,最终会形成一张装机单。和客户确定好装机配置以后,他会把这张单子交给 ///提货的人,由他来准备这些配件,准备完成后交给装机技术人员。技术人员会吧这些配件装成一个整机交给 ///客户 ///不管是什么电脑,它总是有CPU,内存、主板、硬盘以及显卡等部件构成的,并且装机的过程过程总是固定的: ///1、把主板固定在机箱中 ///2、把CPU安装在主板中 ///3、把内存安装到主板上 ///4、把硬盘连接到主板上 ///5、把显卡安装到主板上 ///但是,每台兼容机的部件都各不相同的,有些配置稍高一点。有些配置低一点。这是变化点。对于整机技术人员 ///来说。他不需要考虑这些配件从哪里来的,他只需要把他们组装在一起了。这是稳定的装机流程。要把这种变化的配件和 ///稳定的流程进行分离就需要引入Build模式 namespace Builder_Mo_Shi { /// <summary> /// 电脑城。这里是电脑工厂(指导者) /// </summary> class ComputerFactory { public void BuidComputer(ComputerBuilder cb) { Console.WriteLine(); Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>start building"+cb.Name); cb.SetupCpu(); cb.SetupHarddisk(); cb.SetupMainboard(); cb.SetupMemory(); cb.SetupVideocard(); Console.ReadLine(); } } //电脑构成抽象类 abstract class ComputerBuilder { private string name; public string Name { get { return name; } set { name = value; } } private Computer computer; public Computer Computer { get { return computer; } set { computer = value; } } public ComputerBuilder() { computer = new Computer(); } public abstract void SetupMainboard(); public abstract void SetupCpu(); public abstract void SetupMemory(); public abstract void SetupHarddisk(); public abstract void SetupVideocard(); } //办公室台式机的组装 class officComputerBuilder : ComputerBuilder { public officComputerBuilder() { Name = "OfficeComputer"; } public override void SetupMainboard() { Computer.MainBoard1 = "主板,芯片组要求高档点"; } public override void SetupCpu() { Computer.Cpu1 = "Intel 英特尔赛扬 D 336"; } public override void SetupHarddisk() { Computer.Harddisk = "Hitachi日立SATAII接口台式机硬盘(80G/7200转/8M)盒"; } public override void SetupMemory() { Computer.Memory1 = "Patriot博帝DDR2 667 512MB 台式机内存"; } public override void SetupVideocard() { Computer.Videocard = "主板集成"; } } //游戏机的组装 class GameComputerBuilder : ComputerBuilder { public GameComputerBuilder() { Name = "GameComputer"; } public override void SetupMainboard() { Computer.MainBoard1 = "主板,芯片组要求高档点"; } public override void SetupCpu() { Computer.Cpu1 = "游戏机专用CPU"; } public override void SetupHarddisk() { Computer.Harddisk = "游戏机专用硬盘"; } public override void SetupMemory() { Computer.Memory1 = "Patriot博帝DDR2 667 8T 台式机内存"; } public override void SetupVideocard() { Computer.Videocard = "游戏机主板集成"; } } //电脑实体类 public class Computer { private string videocard; public string Videocard { get { return videocard; } set { videocard = value; } } private string Cpu; public string Cpu1 { get { return Cpu; } set { Cpu = value; } } private string MainBoard; public string MainBoard1 { get { return MainBoard; } set { MainBoard = value; } } private string Memory; public string Memory1 { get { return Memory; } set { Memory = value; } } private string harddisk; public string Harddisk { get { return harddisk; } set { harddisk = value; } } public void ShowSystemInfo() { Console.WriteLine("++++++++++++++++++++++++++++++SystemInfo+++++++++++++++++++++++++++"); Console.WriteLine("CPU:" + Cpu); Console.WriteLine("MainBoard" + MainBoard); Console.WriteLine("HardDisk:" + harddisk); } } } 下面是我们调用该函数的主函数: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Builder_Mo_Shi; using System.Reflection; namespace Builder_Mo_Shi { class Program { static void Main(string[] args) { ComputerFactory factory = new ComputerFactory();//找到电脑商城(指导者) ComputerBuilder offic = new officComputerBuilder();//找到指导人员,选择电脑配置型号 factory.BuidComputer(offic);//找技术员工配置 offic.Computer.ShowSystemInfo();//取走电脑 Console.ReadLine(); } } }
|