C#设计模式五:Builder(含实例源码)

2020-12-11  本文已影响0人  GoodTekken

主程序

static void Main(string[] args)
{
    ComputerFactory factory = new ComputerFactory();

    ComputerBuilder office = new OfficeComputerBuilder();
    factory.BuildComputer(office);
    office.Computer.ShowSystemInfo();

    ComputerBuilder game = new GameComputerBuilder();
    factory.BuildComputer(game);
    game.Computer.ShowSystemInfo();

    Console.ReadKey();
}

ComputerFactory类

class ComputerFactory
{
    public void BuildComputer(ComputerBuilder cb)
    {
        Console.WriteLine();
        Console.WriteLine(">>>>>>>>>>>>>>>>>>Start Building " + cb.Name);
        cb.SetupMainboard();
        cb.SetupCpu();
        cb.SetupMemory();
        cb.SetupHarddisk();
        cb.SetupVideocard();
        Console.WriteLine(">>>>>>>>>>>>>>>>>>Build " + cb.Name + "Completed");
        Console.WriteLine();
    }
}

ComputerBuilder类

abstract class ComputerBuilder
{
    protected string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    protected 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();
}

Computer类

class Computer
{
    private string videocard;
    public string Videocard
    {
        get { return videocard; }
        set { videocard = value; }
    }
    private string cpu;
    public string Cpu
    {
        get {return cpu; }
        set { cpu =value; }
    }
    private string mainboard;
    public string Mainboard
    {
        get { return mainboard; }
        set { mainboard = value; }
    }
    private string memory;
    public string Memory
    {
        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("Memory:" + memory);
        Console.WriteLine("VideoCard:" + videocard);
        Console.WriteLine("HardDisk:" + harddisk); 
    }
}

GameComputerBuilder类

class GameComputerBuilder : ComputerBuilder
{
    public GameComputerBuilder()
    {
        name = "GameComputer";
    }
    public override void SetupMainboard()
    {
        computer.Mainboard = "GIGABYTE技嘉 GA-965P-DS3 3.3主板(INTEL P965东莞产)";
    }
    public override void SetupCpu()
    {
        computer.Cpu = "Intel英特尔酷睿E4400 (2.0GHz/LGA 775/2M/800MHz)盒装";
    }
    public override void SetupMemory()
    {
        computer.Memory = "G.SKILL芝奇F2-6400CL5D-2GBNQ DDR2 800 1G*2台式机内存";
    }
    public override void SetupHarddisk()
    {
        computer.Harddisk = "Hitachi日立SATAII接口台式机硬盘(250G/7200转/8M)盒装";
    }
    public override void SetupVideocard()
    {
        computer.Videocard = "七彩虹逸彩GT-GD3 UP烈焰战神H10显卡(GeForce8600GT / 256M / DDR3)支持HDMI!";
    }

}

OfficeComputerBuilder类

class OfficeComputerBuilder : ComputerBuilder
{
    public OfficeComputerBuilder()
    {
        name = "OfficeComputer";
    }
    public override void SetupMainboard()
    {
        computer.Mainboard = "Abit升技 LG-95C主板(Intel 945GC芯片组/LGA775 / 1066MHz) ";
    }
    public override void SetupCpu()
    {
        computer.Cpu = "Intel英特尔赛扬 D 336 (2.8GHz/LGA 775/256K/533MHz)";
    }
    public override void SetupMemory()
    {
        computer.Memory = "Patriot博帝 DDR2 667 512MB台式机内存";
    }
    public override void SetupHarddisk()
    {
        computer.Harddisk = "Hitachi日立 SATAII接口台式机硬盘(80G/7200转/8M)盒装";
    }
    public override void SetupVideocard()
    {
        computer.Videocard = "主板集成";
    }
}

意图

场景

在电脑城装机总有这样的经历。我们到了店里,先会有一个销售人员来询问你希望装的机器是怎么样的配置,他会给你一些建议,最终会形成一张装机单。
和客户确定了装机配置以后,他会把这张单字交给提货的人,由他来准备这些配件,准备完成后交给装机技术人员。技术人员会把这些配件装成一个整机交给客户。
不管是什么电脑,它总是由CPU、内存、主板、硬盘以及显卡等部件构成的,并且装机的过程总是固定的:

代码说明

何时采用

实现要点

注意事项

C#设计模式一:Singleton(含实例源码)
C#设计模式二:AbstractFactory(含实例源码)
C#设计模式三:FactoryMethod(含实例源码)
C#设计模式四:Prototype(含实例源码)
C#设计模式五:Builder(含实例源码)
C#设计模式六:Adapter(含实例源码)
C#设计模式七:Facade(含实例源码)
C#设计模式八:Proxy(含实例源码)
C#设计模式九:Flyweight(含实例源码)
C#设计模式十:Composite(含实例源码)
C#设计模式十一:Bridge(含实例源码)

上一篇 下一篇

猜你喜欢

热点阅读