设计模式--建造者模式

namespace Builder
{
    //产品
    public class Product
    {
        public string PartA { get; set; }
        public string PartB { get; set; }
        public string PartC { get; set; }

        public  override string ToString()
        {
            return PartA+" "+PartB+" "+PartC;
        }

    }

    //抽象创建者
    public abstract class Builder
    {
        public abstract void CreateA();
        public abstract void CreateB();
        public abstract void CreateC();

        protected Product product=new Product();

        public  Product GetProduct()
        {
            return product;
        }
    }

    //具体创建者
    public class Worker:Builder
    {
        public override void CreateA()
        {
            product.PartA="PartA";
        }
        public override void CreateB()
        {
            product.PartB="PartB";
        }
        public override void CreateC()
        {
            product.PartC="PartC";
        }
    }

    //Director 指挥创建过程
    public class Director
    {
        Builder builder;
        public Product Build()
        {
            builder.CreateA();
            builder.CreateB();
            builder.CreateC();
            return builder.GetProduct();
        }
        public Director( Builder builder)
        {
            this.builder=builder;
        }
    }

    public static class client
    {
        public static void Test()
        {
            Director director=new Director(new Worker());
            Product product=  director.Build();
            Console.WriteLine(product.ToString());
        }
    }
}

Director把建造的流程固化了,也可以不要这个director;直接交给客户端自由搭配

namespace Builder2
{
    //产品
    public class Product
    {
        public string PartA { get; set; }
        public string PartB { get; set; }
        public string PartC { get; set; }

        public  override string ToString()
        {
            return PartA+" "+PartB+" "+PartC;
        }

    }

    //抽象创建者
    public abstract class Builder
    {
        public abstract Builder CreateA(string str);
        public abstract Builder CreateB(string str);
        public abstract Builder CreateC(string str);

        protected Product product=new Product();

        public  Product GetProduct()
        {
            return product;
        }
    }

    //具体创建者
    public class Worker:Builder
    {
        public override Builder CreateA(string str)
        {
            product.PartA=str;
            return this;
        }
        public override Builder CreateB(string str)
        {
            product.PartB=str;
             return this;
        }
        public override Builder CreateC(string str)
        {
            product.PartC=str;
             return this;
        }
    }

    public static class Client
    {
        public static void Test()
        {
            Builder builder=new Worker();
            builder.CreateA("aa")
            .CreateB("bb")
            .CreateC("cc");
            Console.WriteLine(builder.GetProduct().ToString());
        }
    }
}

 

 
posted @ 2020-01-08 22:58  Programmer_Life  阅读(122)  评论(0编辑  收藏  举报