• 设计模式--建造者模式


    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());
            }
        }
    }
     
    SQL Server
  • 相关阅读:
    kingso_sort
    kingso_module
    KINGSO介绍
    kingso
    铁饭碗的含义不是在一个地方永远有饭吃,而是在任何地方都有饭
    立威廉_百度百科
    甜蜜间谍_百度百科
    贝克曼
    报喜鸟集团有限公司_百度百科
    浙江乔治白服饰股份有限公司
  • 原文地址:https://www.cnblogs.com/zlgan/p/12169145.html
Copyright © 2020-2023  润新知