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


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

    建造者模式是一步一步创建一个复杂的对象,它允许用户只通过指定复杂对象的类型和内容就可以构建它们,用户不需要知道内部的具体构建细节。建造者模式属于对象创建型模式。根据中文翻译的不同,建造者模式又可以称为生成器模式。

    建造者模式参与者

    • Builder:是为创建一个产品对象的各个部件指定的抽象接口。
    • ConcreteBuilder:具体建造者,实现Builder接口,构造和装配各个部件。
    • Director:指挥者,是构建一个使用Builder接口的对象
    • Product:具体产品

    建造者模式基本代码

    Product类——产品类由多个部件组成

    namespace BuilderPattern.Structural
    {
        /// <summary>
        /// 产品类
        /// </summary>
        class Product
        {
            IList<string> parts = new List<string>();
            /// <summary>
            /// 添加产品部件
            /// </summary>
            /// <param name="part"></param>
            public void Add(string part)
            {
                parts.Add(part);
            }
            public void Show()
            {
                Console.WriteLine("
    产品 创建----");
                //列举所有产品部件
                foreach (var part in parts)
                {
                    Console.WriteLine(part);
                }
            }
        }
    }

    Builder类——抽象建造者类,确定产品由两个部件PartA和PartB组成,并声明一个得到产品建造后结果的方法GetResult。

    namespace BuilderPattern.Structural
    {
        abstract class Builder
        {
            public abstract void BuilderPartA();
            public abstract void BuilderPartB();
            public abstract Product GetResult();
        }
    }

    ConcreteBuilder1类——具体建造者类。

    namespace BuilderPattern.Structural
    {
        class ConcreteBuilder1 : Builder
        {
            private Product product = new Product();
            /// <summary>
            /// 构建部件A
            /// </summary>
            public override void BuilderPartA()
            {
                product.Add("ConcreteBuilder1 部件A");
            }
            /// <summary>
            /// 构建部件B
            /// </summary>
            public override void BuilderPartB()
            {
                product.Add("ConcreteBuilder1 部件B");
            }
            /// <summary>
            /// 获取产品
            /// </summary>
            /// <returns></returns>
            public override Product GetResult()
            {
                return product;
            }
        }
    }

    ConcreteBuilder2类——具体建造者类。

    namespace BuilderPattern.Structural
    {
        class ConcreteBuilder2 : Builder
        {
            private Product product = new Product();
            public override void BuilderPartA()
            {
                product.Add("部件X");
            }
    
            public override void BuilderPartB()
            {
                product.Add("部件Y");
            }
    
            public override Product GetResult()
            {
                return product;
            }
        }
    }

    Director类——指挥者类。

    namespace BuilderPattern.Structural
    {
        class Director
        {
            public void Construct(Builder builder)
            {
                builder.BuilderPartA();
                builder.BuilderPartB();
            }
        }
    }

    客户端代码,客户不需要知道具体的建造过程。

        static void Main(string[] args)
        {
            try
            {
                Director director = new Director();
                Builder b1 = new ConcreteBuilder1();
                Builder b2 = new ConcreteBuilder2();
    
                director.Construct(b1);
                Product p1 = b1.GetResult();
                p1.Show();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
    }

    结果如下:

    用建造者模式实现交通工具的构造

     交通工具类(Product类)代码如下:

    namespace BuilderPattern.SituationSimulation
    {
        /// <summary>
        /// 交通工具类
        /// </summary>
        class Vehicle
        {
            private Dictionary<string, string> _parts = new Dictionary<string, string>();
    
            public string Name { get; set; }
            public Vehicle(string name)
            {
                this.Name = name;
            }
            /// <summary>
            /// 索引
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public string this[string key]
            {
                get => _parts[key];
                set => _parts[key] = value;
            }
    
            public void Show()
            {
                Console.WriteLine($"
    ------------{this.Name}组件---------------");
                Console.WriteLine($" Frame : {_parts["frame"]}");
                Console.WriteLine($" Engine : {_parts["engine"]}");
                Console.WriteLine($" Wheels: { _parts["wheels"]}");
                Console.WriteLine($" Doors : { _parts["doors"]}");
            }
    
        }
    }

    抽象交通工具部件建造类(Builder类)代码如下:

    namespace BuilderPattern.SituationSimulation
    {
        /// <summary>
        /// 抽象交通工具部件建造类
        /// </summary>
        abstract class VehicleBuilder
        {
            public Vehicle Vehicle { get; set; }
            /// <summary>
            /// 构造车架
            /// </summary>
            public abstract void BuildFrame();
            /// <summary>
            /// 构造改动机
            /// </summary>
            public abstract void BuildEngine();
            /// <summary>
            /// 构造车轮
            /// </summary>
            public abstract void BuildWheels();
            /// <summary>
            /// 构造车门
            /// </summary>
            public abstract void BuildDoors();
        }
    }

    摩托车组件建造类(ConcreteBuilder)代码如下:

    namespace BuilderPattern.SituationSimulation
    {
        /// <summary>
        /// 摩托车组件建造类
        /// </summary>
        class MotorCycleBuilder : VehicleBuilder
        {
            public MotorCycleBuilder()
            {
                Vehicle = new Vehicle("MotorCycle");
            }
            public override void BuildFrame()
            {
                Vehicle["frame"] = "MotorCycle Frame";
            }
    
            public override void BuildEngine()
            {
                Vehicle["engine"] = "MotorCycle Engine 500 cc";
            }
    
            public override void BuildWheels()
            {
                Vehicle["wheels"] = "MotorCycle has 2 Wheels";
            }
    
            public override void BuildDoors()
            {
                Vehicle["doors"] = "MotorCycle has 0 Doors";
            }
            
        }
    }

    汽车组件建造类(ConcreteBuilder)代码如下:

    namespace BuilderPattern.SituationSimulation
    {
        /// <summary>
        /// 汽车组件建造类
        /// </summary>
        class CarCycleBuilder : VehicleBuilder
        {
            public CarCycleBuilder()
            {
                Vehicle = new Vehicle("Car");
            }
            public override void BuildFrame()
            {
                Vehicle["frame"] = "Car Frame";
            }
    
            public override void BuildEngine()
            {
                Vehicle["engine"] = "Car Engine 5000 cc";
            }
    
            public override void BuildWheels()
            {
                Vehicle["wheels"] = "MotorCycle has 4 Wheels";
            }
    
            public override void BuildDoors()
            {
                Vehicle["doors"] = "MotorCycle has 4 Doors";
            }
    
        }
    }

    商店类(Director类)代码如下:

    namespace BuilderPattern.SituationSimulation
    {
        /// <summary>
        /// 商店(指挥者)
        /// </summary>
        class Shop
        {
            public void Construct(VehicleBuilder builder)
            {
                builder.BuildFrame();
                builder.BuildEngine();
                builder.BuildWheels();
                builder.BuildDoors();
            }
        }
    }

    客户端代码:

        static void Main(string[] args)
        {
            try
            {
                    Shop shop = new Shop();
                    VehicleBuilder motorCycleBuilder = new MotorCycleBuilder();
                        
                    shop.Construct(motorCycleBuilder);
                    Vehicle motorCycle = motorCycleBuilder.Vehicle;
                    motorCycle.Show();
    
                    VehicleBuilder carCycleBuilder = new CarCycleBuilder();
                    shop.Construct(carCycleBuilder);
                    Vehicle car = motorCycleBuilder.Vehicle;
                    car.Show();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }

    结果如下:

    源代码地址:https://github.com/houzhenhuang/DesignPattern

  • 相关阅读:
    GMS问题_ThemeHostTest#testThemes
    解压system.img
    Android10_原理机制系列_AMS(ATMS)之应用的第一次启动的过程
    Android10_原理机制系列_AMS之AMS的启动
    如何用云开发高效快捷地实现短信验证码登录?
    推荐一款强大的前端CLI命令行工具
    CloudBase Framework丨第一个 Deno 部署工具是如何打造的?
    实战 | 打造一个双端自动发布的博客体系(下)
    从java注解漫谈到typescript装饰器——注解与装饰器
    TS数据类型:从C++/Java/Python到TS看元组tuple—元组的来龙去脉
  • 原文地址:https://www.cnblogs.com/hhzblogs/p/10375871.html
Copyright © 2020-2023  润新知