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 蔬菜: 西红柿 洋葱 生菜
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)