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(); } }
调用:
Geovin.Du.DuAbstractFactory.Executor duex = new Geovin.Du.DuAbstractFactory.Executor(); duex.Execute();
输出:
请你输入要点的餐数字编号 (1.KungPao,2.Noodles): 1 宫爆鸡丁: 鲜鸡肉,酒,味精,盐,蒜,桂皮,八角等 热量: 899 卡