• CSharp: Builder Pattern in donet 6 (C# 10)


        /// <summary>
        /// 生成器模式 Builder Pattern
        /// </summary>
        public enum MeatType
        {
            Beef,
            Pork,
            Chicken,
            Turkey,
            Salami,
        }
    
        /// <summary>
        /// 生成器模式 Builder Pattern
        /// </summary>
        public enum BreadType
        {
            White,
            Wheat,
            WholeGrain,
            Rye,
        }
    
        /// <summary>
        /// 生成器模式 Builder Pattern
        /// </summary>
        public enum CheeseType
        {
            Gouda,
            Cheddar,
            ParmigianoReggiano,
            Camembert,
            Pule,
        }
    
    
        /// <summary>
        /// 生成器模式 Builder Pattern
        /// </summary>
        public abstract class SandwichBuilder
        {
    
            /// <summary>
            /// 
            /// </summary>
            protected Sandwich _sandwich = new();
    
            /// <summary>
            ///
            /// </summary>
            /// <returns>Sandwich.</returns>
            public Sandwich GetSandwich() => _sandwich;
            /// <summary>
            /// 
            /// </summary>
            public abstract void CreateNewSandwich();
            /// <summary>
            /// 
            /// </summary>
            public abstract void PrepareBread();
            /// <summary>
            /// 
            /// </summary>
            public abstract void ApplyMeatAndCheese();
            /// <summary>
            /// 
            /// </summary>
            public abstract void ApplyVegetables();
            /// <summary>
            /// 
            /// </summary>
            public abstract void AddCondiments();
        }
    
     /// <summary>
        /// 生成器模式 Builder Pattern
        /// </summary>
        public class PremiumSandwichBuilder : SandwichBuilder
        {
    
            /// <summary>
            /// 
            /// </summary>
            public override void CreateNewSandwich() =>
                _sandwich = new Sandwich("优质三明治");
            /// <summary>
            /// 
            /// </summary>
            public override void AddCondiments()
            {
                _sandwich.HasMustard = true;
                _sandwich.HasMayonnaise = true;
                _sandwich.HasKetchup = true;
            }
            /// <summary>
            /// 
            /// </summary>
            public override void ApplyMeatAndCheese()
            {
                _sandwich.MeatType = MeatType.Beef;
                _sandwich.CheeseType = CheeseType.Pule;
            }
            /// <summary>
            /// 
            /// </summary>
            public override void ApplyVegetables() =>
                _sandwich.Vegetables = new List<string> { "西红柿","洋葱","生菜" };
            /// <summary>
            /// 
            /// </summary>
            public override void PrepareBread() =>
                _sandwich.BreadType = BreadType.WholeGrain;
        }
    
    
        /// <summary>
        /// 生成器模式 Builder Pattern
        /// </summary>
        public class CheapSandwichBuilder : SandwichBuilder
        {
            /// <summary>
            /// 
            /// </summary>
            public override void CreateNewSandwich() =>
                _sandwich = new Sandwich("廉价三明治");
            /// <summary>
            /// 
            /// </summary>
            public override void AddCondiments() =>
                _sandwich.HasMayonnaise = true;
            /// <summary>
            /// 
            /// </summary>
            public override void ApplyMeatAndCheese()
            {
                _sandwich.MeatType = MeatType.Salami;
                _sandwich.CheeseType = CheeseType.Cheddar;
            }
            /// <summary>
            /// 
            /// </summary>
            public override void ApplyVegetables() =>
                _sandwich.Vegetables = new List<string> { "西红柿" };
            /// <summary>
            /// 
            /// </summary>
            public override void PrepareBread() =>
                _sandwich.BreadType = BreadType.White;
        }
    
    
     /// <summary>
        /// 生成器模式 Builder Pattern
        /// </summary>
        public class Sandwich
        {
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="name"></param>
            public Sandwich(string name)
            {
                Name = name;
            }
            /// <summary>
            /// 
            /// </summary>
            public Sandwich()
            {
                Name = "Unknown";
            }
            /// <summary>
            /// 
            /// </summary>
            public string Name { get; init; }
            /// <summary>
            /// 
            /// </summary>
            public BreadType BreadType { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public CheeseType CheeseType { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public MeatType MeatType { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public bool IsToasted { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public bool HasMustard { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public bool HasMayonnaise { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public bool HasKetchup { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public List<string> Vegetables { get; set; } = new();
            /// <summary>
            /// 
            /// </summary>
            public void Display()
            {
                Console.WriteLine();
                Console.WriteLine(Name);
                Console.WriteLine("----------------");
    
                Console.WriteLine($"三明治夹 {BreadType} 面包");
    
                if (IsToasted)
                {
                    Console.WriteLine("烤的");
                }
    
                if (HasMayonnaise)
                {
                    Console.WriteLine("用蛋黄酱");
                }
    
                if (HasMustard)
                {
                    Console.WriteLine("和芥末");
                }
    
                if (HasKetchup)
                {
                    Console.WriteLine("加蕃茄酱");
                }
    
                Console.WriteLine($"肉类: {MeatType}", MeatType);
                Console.WriteLine($"奶酪: {CheeseType}", CheeseType);
    
                Console.WriteLine($"蔬菜:");
                foreach (var vegetable in Vegetables)
                {
                    Console.WriteLine($"   {vegetable}");
                }
            }
        }
    
     /// <summary>
        /// 生成器模式 Builder Pattern
        /// </summary>
        public class SandwichDirector
        {
    
            /// <summary>
            /// 
            /// </summary>
            private SandwichBuilder _builder;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="builder"></param>
            public SandwichDirector(SandwichBuilder builder)
            {
                _builder = builder;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="newBuilder"></param>
            public void ChangeBuilder(SandwichBuilder newBuilder) =>
                _builder = newBuilder;
            /// <summary>
            /// 
            /// </summary>
            public void MakeSandwich()
            {
                _builder.CreateNewSandwich();
                _builder.PrepareBread();
                _builder.ApplyMeatAndCheese();
                _builder.ApplyVegetables();
                _builder.AddCondiments();
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public Sandwich GetSandwich() =>
                _builder.GetSandwich();
        }
    
     /// <summary>
        /// 生成器设计模式 Builder Pattern
        /// </summary>
        public static class DuBuilderExecutor 
        {
    
            /// <summary>
            /// 
            /// </summary>
            //public override string Name => "生成器设计模式 Builder Pattern- Creational Pattern";
            /// <summary>
            /// 
            /// </summary>
            public static void Execute()
            {
                var cheapSandwichBuilder = new CheapSandwichBuilder();
                var premiumSandwichBuilder = new PremiumSandwichBuilder();
    
                var director = new SandwichDirector(cheapSandwichBuilder);
                MakeAndDisplaySandwich(director);
    
                // This is not common to see, but it is possible to change builder if necessary.
                director.ChangeBuilder(premiumSandwichBuilder);
                MakeAndDisplaySandwich(director);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="director"></param>
            private static void MakeAndDisplaySandwich(SandwichDirector director)
            {
                director.MakeSandwich();
    
                var sandwich = director.GetSandwich();
                sandwich.Display();
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Geovin.Du.BuildingBlocks;
    
    
    namespace Geovin.Du.DuBuilder
    {
    
    
        /// <summary>
        /// 生成器模式 Builder Pattern
        /// </summary>
        public class Executor : PatternExecutor
        {
            /// <summary>
            /// 
            /// </summary>
            public override string Name => "生成器设计模式 Builder Pattern- Creational Pattern";
            /// <summary>
            /// 邮件执行
            /// </summary>
            public override void Execute()
            {
    
    
                DuBuilderExecutor.Execute();
    
            }
    
        }
    }
    

      

    调用:

     Geovin.Du.DuBuilder.DuBuilderExecutor.Execute();
    

      

    输出:

    Hello, Geovin Du! 学习 .net 6
    
    廉价三明治
    ----------------
    三明治夹 White 面包
    用蛋黄酱
    肉类: Salami
    奶酪: Cheddar
    蔬菜:
       西红柿
    
    优质三明治
    ----------------
    三明治夹 WholeGrain 面包
    用蛋黄酱
    和芥末
    加蕃茄酱
    肉类: Beef
    奶酪: Pule
    蔬菜:
       西红柿
       洋葱
       生菜
    

      

  • 相关阅读:
    算法中时间复杂度概括——o(1)、o(n)、o(logn)、o(nlogn)
    Docker笔记
    struts框架
    引包问题
    官网下载
    WebService
    答辩问题整理
    小程序转发功能的实现
    小程序自定义组件及传值
    vue 点击下拉框
  • 原文地址:https://www.cnblogs.com/geovindu/p/16867714.html
Copyright © 2020-2023  润新知