CSharp: Abstract Factory Pattern in donet 6
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Geovinu.Du.DuAbstractFactory.Meals.Common { /// <summary> ///抽象工厂 模式 Abstract Factory Pattern /// </summary> public interface IMeal { /// <summary> /// /// </summary> void ShowAmountOfCalories(); /// <summary> /// /// </summary> void ShowDescription(); } } /// <summary> ///宫爆鸡丁 Kung Pao Chicken /// </summary> public class KungPao : IMeal { /// <summary> /// 热量 /// </summary> public void ShowAmountOfCalories() { Console.WriteLine( "热量: 899 卡" ); } /// <summary> /// /// </summary> public void ShowDescription() { Console.WriteLine( "宫爆鸡丁: 鲜鸡肉,酒,味精,盐,蒜,桂皮,八角等" ); } } /// <summary> /// 面条 /// </summary> public class Noodles : IMeal { /// <summary> /// 热量 显示卡路里 /// </summary> public void ShowAmountOfCalories() { Console.WriteLine( "热量: 880 卡" ); } /// <summary> /// /// </summary> public void ShowDescription() { Console.WriteLine( "面条: 面,葱姜,老骨汤等." ); } } /// <summary> /// /// </summary> public class NullMeal : IMeal { /// <summary> /// /// </summary> public void ShowAmountOfCalories() { } /// <summary> /// /// </summary> public void ShowDescription() { } } using Geovin.Du.DuAbstractFactory.Meals; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Geovinu.Du.DuAbstractFactory.Meals.Common; namespace Geovin.Du.DuAbstractFactory.Factories { /// <summary> /// /// </summary> public class MealFactory { /// <summary> /// /// </summary> private Dictionary< string , Type> _meals; /// <summary> /// /// </summary> public MealFactory() { LoadAvailableMealTypes(); } /// <summary> /// /// </summary> /// <param name="mealName"></param> /// <returns></returns> public IMeal CreateMeal( string mealName) { var type = GetTypeForCreation(mealName); if (type == null ) { return new NullMeal(); } return Activator.CreateInstance(type) as IMeal; } /// <summary> /// /// </summary> /// <param name="check"></param> /// <returns></returns> public IMeal CreateMealInt( string check) { string mealName = check== "1" ? "kungpao" : "noodles" ; //if (check == "0") // mealName = "KungPao"; //if (check == "1") // mealName = "KungPao"; //if (check == "2") // mealName = "Noodles"; var type = GetTypeForCreation(mealName); if (type == null ) { return new NullMeal(); } return Activator.CreateInstance(type) as IMeal; } /// <summary> /// /// </summary> /// <param name="mealName"></param> /// <returns></returns> private Type GetTypeForCreation( string mealName) { if (!_meals.TryGetValue(mealName, out Type type)) { return null ; } return type; } /// <summary> /// /// </summary> private void LoadAvailableMealTypes() { _meals = new Dictionary< string , Type>(); Type[] assemblyTypes = Assembly.GetExecutingAssembly().GetTypes(); foreach (Type type in assemblyTypes) { if (type.GetInterface( typeof (IMeal).ToString()) != null ) { _meals.Add(type.Name.ToLower(), type); } } } } } /// <summary> /// /// </summary> public class Executor : PatternExecutor { /// <summary> /// /// </summary> public override string Name => "/抽象工厂 模式 Abstract Factory Pattern (Simple Factory) - Creational Pattern" ; /// <summary> /// /// </summary> public override void Execute() { Console.WriteLine( "请你输入要点的餐数字编号 (1.KungPao,2.Noodles): " ); string mealName = Console.ReadLine(); MealFactory factory = new MealFactory(); IMeal meal = factory.CreateMealInt(mealName); //IMeal meal = factory.CreateMeal(mealName); meal.ShowDescription(); meal.ShowAmountOfCalories(); } } |
调用:
1 2 | Geovin.Du.DuAbstractFactory.Executor duex = new Geovin.Du.DuAbstractFactory.Executor(); duex.Execute(); |
输出:
1 2 3 4 | 请你输入要点的餐数字编号 (1.KungPao,2.Noodles): 1 宫爆鸡丁: 鲜鸡肉,酒,味精,盐,蒜,桂皮,八角等 热量: 899 卡 |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
标签:
desgin patterns
, 设计模式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!