• 建造者模式(Builder)


    建造者模式:将一个复杂对象的构建与他的表示分离,使得同样的构建过程可以创建不同的表示。

    Builder   Director

    Builder是什么?是为Product创建一个对象的各个部件指定的抽象接口,

    ContreteBuilder:是具体建造者,实现Builder接口,构造和装配各个部件。Product就是具体的对象。

    Director是指挥者,构建一个使用builder接口的对象。用于构建一些复杂的对象,这些对象内部构建间的构造顺序通常是稳定的,但对象的构造通常面临着复杂的变化。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.BuilderPattern.Structural
    {
        public class Product
        {
            private List<string> _parts = new List<string>();
    
            public void Add(string part)
            {
                _parts.Add(part);
            }
    
            public void Show()
            {
                Console.WriteLine("Product Parts");
                foreach (string part in _parts)
                {
                    Console.WriteLine(part);
                }
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.BuilderPattern.Structural
    {
        public abstract class Builder
        {
            public abstract void BuildPartA();
            public abstract void BuildPartB();
            public abstract Product GetResult();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.BuilderPattern.Structural
    {
        public class ConcreteBuilder1 : Builder
        {
            private Product _product = new Product();
    
            public override void BuildPartA()
            {
                _product.Add("PartA");
            }
    
            public override void BuildPartB()
            {
                _product.Add("PartB");
            }
    
            public override Product GetResult()
            {
                return _product;
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.BuilderPattern.Structural
    {
        public class ConcreteBuilder2 : Builder
        {
            private Product _product = new Product();
    
            public override void BuildPartA()
            {
                _product.Add("PartX");
            }
    
            public override void BuildPartB()
            {
                _product.Add("PartY");
            }
    
            public override Product GetResult()
            {
                return _product;
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.BuilderPattern.Structural
    {
        public class Director
        {
            /// <summary>
            /// Builder uses a complex series of steps
            /// </summary>
            public void Construct(Builder builder)
            {
                builder.BuildPartA();
                builder.BuildPartB();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using DesignPatterns.BuilderPattern.Structural;
    
    namespace DesignPatterns.BuilderPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Create director and builders
                Director director = new Director();
    
                Builder b1 = new ConcreteBuilder1();
                Builder b2 = new ConcreteBuilder2();
                
                // Construct two products
                director.Construct(b1);
                Product p1 = b1.GetResult();
                p1.Show();
    
                director.Construct(b2);
                Product p2 = b2.GetResult();
                p2.Show();
            }
        }
    }


    建造者模式(Builder) http://www.cnblogs.com/libingql/p/3633527.html
  • 相关阅读:
    【html、CSS、javascript-9】jquery-选择器及过滤器
    【python之路40】Python 作用域
    H5缓存
    解决网络不可用--Using_Service_Workers
    跨域请求CORS
    基于node的websocket示例
    test
    函数节流
    ES6 promise
    web前端免费资源集
  • 原文地址:https://www.cnblogs.com/suzixuan/p/6995562.html
Copyright © 2020-2023  润新知