首页 > 代码库 > C# 设计模式巩固笔记 - 建造者模式

C# 设计模式巩固笔记 - 建造者模式

前言

写给自己-贵在坚持。建造者模式不复杂,但是想个形象的例子好难。

介绍-建造者模式

定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

实现

建造者模式主要是应对复杂对象,在之前讲的滑板鞋的例子看上去不是很适合,因为它并不复杂。但是我们把一双鞋子拆分一下:鞋底、鞋带、鞋面 等等。原来它也并不简单,下面通过建造者模式来实现一下造鞋子的流程:

首先是建造者类:

    /// <summary>
    /// 建造者基类
    /// </summary>
    public abstract class Builder
    {
        /// <summary>
        /// 组装鞋底
        /// </summary>
        public abstract void BuildTread();
        /// <summary>
        /// 组装鞋面
        /// </summary>
        public abstract void BuildVamp();
        /// <summary>
        /// 穿鞋带
        /// </summary>
        public abstract void BuildShoeLace();
        /// <summary>
        /// 鞋子出厂
        /// </summary>
        /// <returns></returns>
        public abstract Shoes OutShoes();
    }

工厂里谁是建造者,当然是工人:

  /// <summary>
    /// 操作员1
    /// </summary>
    public class Operator1 : Builder
    {
        Shoes shoes = new Shoes();
        public override void BuildShoeLace()
        {
            shoes.AddPart("鞋带1");
        }

        public override void BuildTread()
        {
            shoes.AddPart("鞋底1");
        }

        public override void BuildVamp()
        {
            shoes.AddPart("鞋面1");
        }

        public override Shoes OutShoes()
        {
            return shoes;
        }
    }
    
    /// <summary>
    /// 操作员2
    /// </summary>
    public class Operator2 : Builder
    {
        Shoes shoes = new Shoes();
        public override void BuildShoeLace()
        {
            shoes.AddPart("鞋带2");
        }

        public override void BuildTread()
        {
            shoes.AddPart("鞋底2");
        }

        public override void BuildVamp()
        {
            shoes.AddPart("鞋面2");
        }

        public override Shoes OutShoes()
        {
            return shoes;
        }
    }

还是产品-鞋子:

  /// <summary>
    /// 鞋子
    /// </summary>
    public class Shoes
    {
        public Shoes()
        {
            Parts = new List<string>();
        }
        List<string> Parts { get; set; }

        public void AddPart(string part)
        {
            Parts.Add(part);
        }
        public void ShowParts()
        {
            for(int i =0,c = Parts.Count;i < c; i++)
            {
                Console.WriteLine("我的滑板鞋建造的顺序是_{0}:{1}",i,Parts[i]);
            }
        }
    }

假如是你工人的话,你会自觉的干活吗?(我不会,嘿嘿...所以需要老板来指挥):

   public class Boss
    {
        public Shoes BuidShoes(Builder builder)
        {
            builder.BuildTread();
            builder.BuildVamp();
            builder.BuildShoeLace();
            return builder.OutShoes();
        }
    }

下面看调用:

namespace DesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Boss boss = new Boss();
            Builder builder1 = new Operator1();
            Shoes shoes1 = boss.BuidShoes(builder1);
            shoes1.ShowParts();
            Console.WriteLine("-----------------华丽的分割线-----------------");
            Builder builder2 = new Operator2();
            Shoes shoes2 = boss.BuidShoes(builder2);
            shoes2.ShowParts();

            Console.Read();
        }
    }
}

运行结果:

技术分享

 

欢迎批评指正,转载请注明出处 http://www.cnblogs.com/xinwang/p/6380564.html

 

C# 设计模式巩固笔记 - 建造者模式