设计模式学习总结2 - 创建型2 - Builder生成器模式

Builder生成器模式(创建型)

作用:

Builder生成器模式将生成复杂对象的具体过程和它的构造函数分离开,同样的构造过程可以实例出不同的对象。

Role
The Builder pattern separates the specification of a complex object from its actual construction. The same construction process can create different representations.

设计:

设计模式学习总结2 - 创建型2 - Builder生成器模式_第1张图片

生成器接口IBuilder,定义要Build哪些内容
实例对象控制器Director,包含一系列生成对象的操作
生成器Builder,被实例化对象控制器Director调用来实例化对象
对象Product,由多部分组成的对象

举例:
IBuilder   制造钢笔的一系列规范
Director  使用BuilderA制造钢笔
BuilderA  某种按IBuilder规范制造钢笔的方法
Product   由多个零件组成的钢笔
代码:

 

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Builder
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;

    namespace BuilderExemple
    {
        class Program
        {
            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();
            }
        }

        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();
            }
        }

        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();
        }

        class OfficeComputerBuilder : ComputerBuilder
        {
            public OfficeComputerBuilder()
            {
                name = "OfficeComputer";
            }

            public override void SetupMainboard()
            {
                computer.Mainboard = "Abit升技 LG-95C 主板 (Intel 945GC芯片组/LGA 775/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 = "主板集成";
            }
        }

        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 英特尔酷睿2 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 = "七彩虹 逸彩8600GT-GD3 UP烈焰战神 H10 显卡 (GeForce 8600GT/256M/DDR3)支持HDMI!";
            }
        }

        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);
                Console.ReadLine();
            }
        }
    }
}

 

使用场景:

创建对象的各个组成部分的算法是可以分离的;
组合起来的对象可能会有不同的表现
需要控制对象的构造过程

Use the Builder pattern when…
•  The algorithm for creating parts is independent from the parts themselves.
•  The object to be assembled might have different representations.
•  You need fine control over the construction process.

 

总结:

Builder生成器模式是一种创建型模式,它主要是应对项目中一些复杂对象的创建工作。所谓“复杂对象”,是指:此对象中还含有其它的子对象。Builder模式应有的情景:复杂对象的各个部分的子对象可以用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将各个部分的子对象组合在一起的算法却相对稳定。简单的说:子对象变化较频繁,对组合子对象算法相对稳定。
    这是解决一个复杂对象的创建工作,现在变化的部分和相对稳定的部分已经明确,我们要做的是隔离变化,如何将子对象和算法隔离是要解决的问题。
    《设计模式》中说道:将一个复杂对象的构建与其表示向分离,使得同样的构建过程可以创建不同的表示。Builder模式的几个要点:
    1、用于构建一个复杂的对象,但这个对象构建的算法是稳定的,对象中的各个部分经常变化。
    2、在于应对复杂对象各个部分的频繁需求变动。但是难以应对算法的需求变动。这点一定要注意,如果用复杂对象的内部子对象的组合算法不稳定,使用Builder会带来很多不必要的麻烦。

你可能感兴趣的:(builder)