为了应对复杂对象的创建,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法确相对稳定。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | using System; using System.Collections; using System.Reflection; namespace DapperTest.Models { // Food类,即产品类 public class Food { Hashtable food = new Hashtable(); // 添加食品 public void Add( string strName, string Price) { food.Add(strName, Price); } // 显示食品清单 public void Show() { IDictionaryEnumerator myEnumerator = food.GetEnumerator(); Console.WriteLine( "Food List:" ); Console.WriteLine( "------------------------------" ); string strfoodlist = "" ; while (myEnumerator.MoveNext()) { strfoodlist = strfoodlist + "\n\n" + myEnumerator.Key.ToString(); strfoodlist = strfoodlist + ":\t" + myEnumerator.Value.ToString(); } Console.WriteLine(strfoodlist); Console.WriteLine( "\n------------------------------" ); } } // FoodManager类,即指导者 public class FoodManager { public void Construct(Builder builder) { builder.BuildHamb(); builder.BuildCoke(); builder.BuildChip(); } } // Builder类,即抽象建造者类,构造套餐 public abstract class Builder { // 添加汉堡 public abstract void BuildHamb(); // 添加可乐 public abstract void BuildCoke(); // 添加薯条 public abstract void BuildChip(); // 返回结果 食品对象 public abstract Food GetFood(); } // NormalBuilder类,具体构造者,普通套餐 public class NormalBuilder : Builder { private Food NormalFood = new Food(); public override void BuildHamb() { NormalFood.Add( "NormalHamb" , "¥10.00" ); } public override void BuildCoke() { NormalFood.Add( "CokeCole" , "¥4.50" ); } public override void BuildChip() { NormalFood.Add( "FireChips" , "¥2.00" ); } public override Food GetFood() { return NormalFood; } } // GoldBuilder类,具体构造者,黄金套餐 public class GoldBuilder : Builder { private Food GoldFood = new Food(); public override void BuildHamb() { GoldFood.Add( "GoldHamb" , "¥25.00" ); } public override void BuildCoke() { GoldFood.Add( "CokeCole" , "¥4.50" ); } public override void BuildChip() { GoldFood.Add( "FireChips" , "¥3.50" ); } public override Food GetFood() { return GoldFood; } } //调用 public class BuilderPattern { public static void BuildTest() { FoodManager foodmanager = new FoodManager(); Console.WriteLine( "Please Enter Food No:" ); string foodType = Console.ReadLine(); if (foodType == "1" ) { foodType = "NormalBuilder" ; } if (foodType == "2" ) { foodType = "GoldBuilder" ; } Builder instance = (Builder)Assembly.Load( "DapperTest" ).CreateInstance( "DapperTest.Models." + foodType); foodmanager.Construct(instance); Food food = instance.GetFood(); food.Show(); } } } |
分类:
DesignMode(设计模式)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2016-07-08 js定时器 setTimeout 和 setInterval 区别
2016-07-08 js日期操作,js日期戳转日期