static void Main(string[] args) { Console.WriteLine("Hello World!"); Console.WriteLine("--------------------FactoryMethodPattern--------------------"); Console.WriteLine(""); #region 简单工厂 var food1 = FoodSimpleFactory.CreateFood("西红柿炒蛋"); food1.Print(); var food2 = FoodSimpleFactory.CreateFood("土豆肉丝"); food2.Print(); #endregion #region 工厂方法 //客户端调用 //初始化红旗汽车工厂 var hongQiCarFactory = new HongQiCarFactory(); //生产宏碁汽车 var hongQiCar = hongQiCarFactory.CreateCar(); hongQiCar.Go(); var aoDiCarFactory = new AoDiCarFactory(); var aoDiCar = aoDiCarFactory.CreateCar(); aoDiCar.Go(); var benChiCarFactory = new BenChiCarFactory(); var benChiCar = benChiCarFactory.CreateCar(); benChiCar.Go(); #endregion Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("非简单工厂模式实现,实现顾客自己在家做菜,顾客和菜之间有耦合。"); Food food1 = Customer.Cook("西红柿炒蛋"); food1.Print(); Food food2 = Customer.Cook("土豆肉丝"); food2.Print(); Console.WriteLine("————————————"); Console.WriteLine("非简单工厂模式实现,要什么菜自己生产,所以和菜之间有耦合。"); Food food3 = new TomatoScrambledEggs(); food3.Print(); Food food4 = new ShreddedPorkWithPotatoes(); food4.Print(); Console.WriteLine("————————————"); Console.WriteLine("简单工厂模式实现,要什么菜有工厂提供,和菜之间没有耦合。"); Food food5 = FoodSimpleFactory.CreateFood("西红柿炒蛋"); food5.Print(); Food food6 = FoodSimpleFactory.CreateFood("土豆肉丝"); food6.Print(); Console.Read(); }
public void CreateFoodTest03() { // 客户想点一份红烧肉 BaseFood food = FoodSimpleFactory.CreateFood("红烧肉"); // 大厨堵在路上(无法提供所需要的菜) Assert.IsNull(food); }
public void CreateFoodTest02() { // 客户想点一份土豆肉丝 BaseFood food = FoodSimpleFactory.CreateFood("土豆肉丝"); food.PutInto(); Assert.AreEqual("一份土豆肉丝", food.Dish); }
public void CreateFoodTest01() { // 客户想点一个西红柿炒蛋 BaseFood food = FoodSimpleFactory.CreateFood("西红柿炒蛋"); food.PutInto(); Assert.AreEqual("一份西红柿炒蛋", food.Dish); }
private void button1_Click(object sender, EventArgs e) { // 做西红柿炒蛋 Food food1 = FoodSimpleFactory.CreateFood("西红柿炒蛋"); food1.Print(); Food food2 = FoodSimpleFactory.CreateFood("土豆肉丝"); food1.Print(); Console.Read(); }
public IActionResult Index() { Food food1 = FoodSimpleFactory.CreateFood("西红柿炒鸡蛋"); food1.Cook(); Food food2 = FoodSimpleFactory.CreateFood("土豆丝"); food2.Cook(); return(View()); }
static void __Main(string[] args) { // 客户想点一个西红柿炒蛋 Food food1 = FoodSimpleFactory.CreateFood("西红柿炒蛋"); food1.Print(); // 客户想点一个土豆肉丝 Food food2 = FoodSimpleFactory.CreateFood("土豆肉丝"); food2.Print(); Console.Read(); }
public static void Main(string[] args) { Console.WriteLine("这里是简单工厂"); var foodSimpleFactory = new FoodSimpleFactory(); foodSimpleFactory.Cook("菠萝油").Show(); foodSimpleFactory.Cook("干炒牛河").Show(); foodSimpleFactory.Cook("罗宋汤").Show(); foodSimpleFactory.Cook("丝袜奶茶").Show(); foodSimpleFactory.Cook("猪扒包").Show(); Console.ReadKey(); }
private void button2_Click(object sender, EventArgs e) { // 客户想点一个西红柿炒蛋 Food food1 = FoodSimpleFactory.CreateFood("西红柿炒蛋"); food1.Print(); // 客户想点一个土豆肉丝 Food food2 = FoodSimpleFactory.CreateFood("土豆肉丝"); food2.Print(); string className = "gps"; button2_NoEncapsulation(className); button2_Encapsulation(className); className = "imu"; button2_NoEncapsulation(className); button2_Encapsulation(className); }
static void Main(string[] args) { #region normal pattern of program { //Console.WriteLine("*********************1 normal pattern of program****************"); //AbstractFood braisedPolkBall = new BraisedPolkBall(); //braisedPolkBall.ShowBasicInfo(); //braisedPolkBall.ShowCookMethod(); //braisedPolkBall.Taste(); //AbstractFood crabPackage = new CrabPackage(); //crabPackage.ShowBasicInfo(); //crabPackage.ShowCookMethod(); //crabPackage.Taste(); //AbstractFood squirrelFish = new SquirrelFish(); //squirrelFish.ShowBasicInfo(); //squirrelFish.ShowCookMethod(); //squirrelFish.Taste(); //Console.WriteLine("********************* End Of normal Pattern*******************************"); } #endregion #region simple factory { //Console.WriteLine("*********************2 simple factory, create instance with Enum****************"); //Console.WriteLine("******simple factory: create instance based on Enum, call different new Class() "); //AbstractFood braisedPolkBall = FoodSimpleFactory.CreateInstanceByNormal(FoodTypeEnum.BraisedPolkBall); //braisedPolkBall.ShowBasicInfo(); //braisedPolkBall.ShowCookMethod(); //braisedPolkBall.Taste(); //Console.WriteLine("************create instance from Config, then to Enum, then use simple factory "); //AbstractFood crabPackage = FoodSimpleFactory.CreateInstanceByConfig(); //crabPackage.ShowBasicInfo(); //crabPackage.ShowBasicInfo(); //crabPackage.Taste(); //Console.WriteLine("************create instance from Config, then use reflection to create class "); //AbstractFood squirrelFish = FoodSimpleFactory.CreateInstanceByReflection(); //squirrelFish.ShowBasicInfo(); //squirrelFish.ShowCookMethod(); //squirrelFish.Taste(); //Console.WriteLine("********************* End Of simple factory*******************************"); } #endregion #region Factory Method { //Console.WriteLine("**********************3 Factory Method*************************************"); //BaseFactory braisedPolkBallFactory = new BraisedPolkBallFactory(); //AbstractFood braisedPolkBall = braisedPolkBallFactory.CreateInstance(); //braisedPolkBall.ShowBasicInfo(); //braisedPolkBall.ShowCookMethod(); //braisedPolkBall.Taste(); //Console.WriteLine("**********************End of Factory Method*************************************"); } #endregion #region Abstract Factory { //Console.WriteLine("**********************4 Abstract Factory*************************************"); //IHuaiYangFoodAbstractFactory factory = new HuaiYangFoodAbstractFactory(); //Console.WriteLine("1 first dish"); //AbstractFood food = factory.CreateBraisedPolkBall(); //food.ShowBasicInfo(); //food.ShowCookMethod(); //food.Taste(); //Console.WriteLine("2 a soup"); //AbstractSoup soup = factory.CreateTomatoEggSoup(); //soup.ShowBasicInfo(); //soup.Taste(); //Console.WriteLine("**********************End of Abstract Factory*************************************"); } #endregion #region Console Menu { //Console.WriteLine("**********************5 Console Menu*************************************"); //Console.WriteLine("*****************************************************"); //FoodMenu menu = FoodMenu.CreateInstance(); //if (menu.FoodList != null && menu.FoodList.Count > 0) //{ // foreach (FoodModel model in menu.FoodList) // { // Console.WriteLine(string.Format("ID: {0}{1} Price: {2} Score: {3} ", // model.FoodId, // model.FoodName, // string.Format("{0:C}",model.Price), // model.FoodScore // ) ); // Console.WriteLine("*****************************************************"); // } //} //Console.WriteLine("please type in food id and press enter to continue..."); //while (true) //{ // if (!int.TryParse(Console.ReadLine(), out int input)) // { // LogHelper.WriteInfoLog("your input is not int, please try again", ConsoleColor.Red); // } // else // { // var selectedFood = menu.FoodList.FirstOrDefault(c => c.FoodId == input); // if (selectedFood == null) // { // LogHelper.WriteInfoLog("Sorry, we don't have that!",ConsoleColor.Red); // } // else // { // string msg = string.Format("You select dish: {0}, price: {1}, Score: {2}, price: {3}", // selectedFood.FoodId, // selectedFood.FoodName, // selectedFood.FoodScore, // string.Format("{0:C}",selectedFood.Price ) // ); // LogHelper.WriteInfoLog(msg, ConsoleColor.DarkGreen); // AbstractFood absFood = FoodSimpleFactory.CreateInstanceByReflectionInfo(selectedFood.SimpleFactory); // absFood.ShowBasicInfo(); // absFood.ShowCookMethod(); // break; // } // } //} //Console.WriteLine("**********************End of Console Menu*************************************"); } #endregion #region Single-thread order /* * { * Console.WriteLine("******************Single-thead order***************************"); * char[] tips = "Please see below".ToCharArray(); * for (int i = 0; i < tips.Length; i++) * { * Console.Write(tips[i]); * Thread.Sleep(100); * } * * Console.WriteLine(); * * FoodMenu menu = FoodMenu.CreateInstance(); * * OrderInfoList orderInfoList = OrderInfoList.CreateInstance(); * OrderModel order = orderInfoList._orderModel; * string msg1 = string.Format("{0} come to order. ", string.Join(", ", order.CustomerList)); * LogHelper.WriteInfoLog(msg1, ConsoleColor.DarkRed); * * * //List<Task> taskList = new List<Task>(); * Dictionary<string, Dictionary<AbstractFood, int>> dictionaryAll = * new Dictionary<string, Dictionary<AbstractFood, int>>(); * * List<Dictionary<AbstractFood, int>> allCustomerScoreDicList = new List<Dictionary<AbstractFood, int>>(); * foreach (var item in order.CustomerList) * { * allCustomerScoreDicList.Add(new Dictionary<AbstractFood, int>()); * } * * int k = 0; * foreach (string customer in order.CustomerList) * { * Dictionary<AbstractFood, int> oneCustomerScoreDic = allCustomerScoreDicList[k++]; * * * //taskList.Add( * // Task.Run( * // () => * { * List<FoodModel> orderList = menu.GetFoodListByRandom(); * string orderMsg = string.Format("Customer: {0} order these: {1}", customer, * string.Join(",", orderList.Select(c => c.FoodName))); * LogHelper.WriteInfoLog(orderMsg, ConsoleColor.DarkRed); * * foreach (FoodModel food in orderList) * { * AbstractFood foodChosen = * FoodSimpleFactory.CreateInstanceByReflectionInfo(food.SimpleFactory); * foodChosen.BaseFood.CustomerName = customer; * foodChosen.Cook(); * foodChosen.Taste(); * int score = foodChosen.Score(); * oneCustomerScoreDic.Add(foodChosen, score); * } * * int maxScore = oneCustomerScoreDic.Values.Max(); * foreach (var item in oneCustomerScoreDic.Where(d => d.Value == maxScore)) * { * Console.BackgroundColor = ConsoleColor.DarkRed; * Console.WriteLine( * $"@@@@@@{customer} 's favourite food is {item.Key.BaseFood.FoodName}" + * $", score is {item.Value} @@@@@@"); * Console.BackgroundColor = ConsoleColor.Black; * } * * * * } * // ) * // ); * } * * //Task.WaitAll(taskList.ToArray()); * Console.WriteLine("*****************All Customers' favourite*********************************"); * int maxAll = allCustomerScoreDicList.Max(d => d.Values.Max()); * for (int i = 0; i < order.CustomerList.Count; i++) * { * var dic = allCustomerScoreDicList[i]; * foreach (var item in dic.Where(d => d.Value == maxAll)) * { * Console.WriteLine($"{order.CustomerList[i]} " + * $"s favourite food is {item.Key.BaseFood.FoodName}" + * $", score is {item.Value}"); * } * } * } #endregion * * * #region multi-thread order * { * Console.WriteLine("******************Multi-thead order***************************"); * char[] tips = "Please see below".ToCharArray(); * for (int i = 0; i < tips.Length; i++) * { * Console.Write(tips[i]); * Thread.Sleep(100); * } * * Console.WriteLine(); * * FoodMenu menu = FoodMenu.CreateInstance(); * * OrderInfoList orderInfoList = OrderInfoList.CreateInstance(); * OrderModel order = orderInfoList._orderModel; * string msg1 = string.Format("{0} come to order. ", string.Join(", ", order.CustomerList)); * LogHelper.WriteInfoLog(msg1, ConsoleColor.DarkRed); * * * List<Task> taskList = new List<Task>(); * Dictionary<string, Dictionary<AbstractFood, int>> dictionaryAll = * new Dictionary<string, Dictionary<AbstractFood, int>>(); * * List<Dictionary<AbstractFood, int>> allCustomerScoreDicList = new List<Dictionary<AbstractFood, int>>(); * //must have a container containing multiple containers for each customer. * //if use multi-thread, sharing one container will cause issue, lock will affect efficiency. * //so must use separate container for each thread. * foreach (var item in order.CustomerList) * { * allCustomerScoreDicList.Add(new Dictionary<AbstractFood, int>()); * } * * int k = 0; * foreach (string customer in order.CustomerList) * { * Dictionary<AbstractFood, int> oneCustomerScoreDic = allCustomerScoreDicList[k++]; * * * taskList.Add( * Task.Run( * () => * { * List<FoodModel> orderList = menu.GetFoodListByRandom(); * string orderMsg = string.Format("Customer: {0} order these: {1}", customer, * string.Join(",", orderList.Select(c => c.FoodName))); * LogHelper.WriteInfoLog(orderMsg, ConsoleColor.DarkRed); * * foreach (FoodModel food in orderList) * { * AbstractFood foodChosen = * FoodSimpleFactory.CreateInstanceByReflectionInfo(food.SimpleFactory); * foodChosen.BaseFood.CustomerName = customer; * foodChosen.Cook(); * foodChosen.Taste(); * int score = foodChosen.Score(); * oneCustomerScoreDic.Add(foodChosen, score); * } * * int maxScore = oneCustomerScoreDic.Values.Max(); * foreach (var item in oneCustomerScoreDic.Where(d => d.Value == maxScore)) * { * Console.BackgroundColor = ConsoleColor.DarkRed; * Console.WriteLine( * $"@@@@@@{customer} 's favourite food is {item.Key.BaseFood.FoodName}" + * $", score is {item.Value} @@@@@@"); * Console.BackgroundColor = ConsoleColor.Black; * } * } * ) * ); * } * * Task.WaitAll(taskList.ToArray()); // wait for every task finish eating. * Console.WriteLine("*****************All Customers' favourite*********************************"); * int maxAll = allCustomerScoreDicList.Max(d => d.Values.Max()); * for (int i = 0; i < order.CustomerList.Count; i++) * { * var dic = allCustomerScoreDicList[i]; * foreach (var item in dic.Where(d => d.Value == maxAll)) * { * Console.WriteLine($"{order.CustomerList[i]} " + * $"s favourite food is {item.Key.BaseFood.FoodName}" + * $", score is {item.Value}"); * } * } * } */ #endregion #region decorator { Console.WriteLine("**********************Decorator************************************"); AbstractFood food = FoodSimpleFactory.CreateInstanceByNormal(FoodTypeEnum.BraisedPolkBall); //do before base.Cook(); food = new FoodDecoratorCut(food); food = new FoodDecoratorClean(food); //do after base.Cook(); food = new FoodDecoratorPlace(food); food = new FoodDecoratorShow(food); //do before base.Cook(); //even put at the end, execute before base.Cook() food = new FoodDecoratorBuy(food); food.Cook(); } #endregion #region observer /* * { * AbstractFood food = FoodSimpleFactory.CreateInstanceByNormal(FoodTypeEnum.BraisedPolkBall); * food.PerfactScoreHandle += () => * { * Console.WriteLine("Customer buy...."); * }; * * food.PerfactScoreHandle += () => { Console.WriteLine("Journalist1 report....."); }; * * food.PerfactScoreHandle += () => { Console.WriteLine("Journalist2 report......"); }; * * food.Score(5); * * } * */ #endregion Console.ReadKey(); }
static void Main(string[] args) { #region 1 每个人要学会做几个菜,不低于3个。。。先不用任何工厂方法,普通实现,分别展示几个菜,好不好吃 Console.WriteLine("+++++++++++++++++++每个人要学会做几个菜,不低于3个。。。先不用任何工厂方法,普通实现,分别展示几个菜,好不好吃++++++++++++++"); { ///模拟每个人炒菜 var person = new Person(); var tomatoegg = new TomatoScrambledEggs(); var vegetable = new Vegetable(); var fish = new Fish(); ///番茄炒蛋 person.Cook(tomatoegg); ///青菜 person.Cook(vegetable); ///鱼 person.Cook(fish); ///自己做得菜,好不好吃 person.Show(); } #endregion #region 2 用简单工厂实现客人点菜,而不是让客人自己做菜 Console.WriteLine("++++++++++++++++++++用简单工厂实现客人点菜,而不是让客人自己做菜+++++++++++++++++++++++++++++++++++++++++"); { var tomatoegg = FoodSimpleFactory.CreateFood(FoodType.TomatoScrambledEggs); tomatoegg.Show(); var fish = FoodSimpleFactory.CreateFood(FoodType.Fish); fish.Show(); var vegetable = FoodSimpleFactory.CreateFood(FoodType.Vegetable); vegetable.Show(); } #endregion #region 3 用工厂方法实现客人点菜,而不是让客人自己做菜 Console.WriteLine("++++++++++++++++++++用工厂方法实现客人点菜,而不是让客人自己做++++++++++++++++++++++++++++++++++++++++++"); { var tomatoScrambledEggsFactory = new SecondModel.Factory.TomatoScrambledEggsFactory(); var tomatoegg = tomatoScrambledEggsFactory.CreateFoodFactory(); tomatoegg.Show(); var fishFactory = new SecondModel.Factory.FishFactory(); var fish = fishFactory.CreateFoodFactory(); fish.Show(); var vegatableFactory = new SecondModel.Factory.VegetableFactory(); var vegatable = vegatableFactory.CreateFoodFactory(); vegatable.Show(); } #endregion #region 4 用抽象工厂,每个工厂都能做三个菜、一个汤、一个主食 Console.WriteLine("++++++++++++++++++++用抽象工厂,每个工厂都能做三个菜、一个汤、一个主食++++++++++++++++++++++++++++++++++"); { var meal = new ThirdModel.Meal(); meal.Show(); } #endregion #region 5 做个点菜系统,用户输入可选菜id进行点菜: #region 多线程演示:甲乙丙三个客人(三个线程)分别随机点5个菜,然后每个菜依次做菜、品尝、点评 try { Console.WriteLine("++++++++++++++++++++多线程演示:甲乙丙三个客人(三个线程)分别随机点5个菜,然后每个菜依次做菜、品尝、点评++"); { List <Client> clients = new List <Client>(); Client client = new Client("甲"); clients.Add(client); client = new Client("乙"); clients.Add(client); client = new Client("丙"); clients.Add(client); Parallel.ForEach(clients, c => { c.Show(); }); Client.MaxScore(); } } catch (AggregateException aex) { foreach (var ex in aex.InnerExceptions) { LogHelper.WriteLog(ex.Message); } } catch (Exception ex) { LogHelper.WriteLog(ex.Message); } #endregion #region 用户输入id点菜 try { Console.WriteLine("++++++++++++++++++++做个点菜系统,用户输入可选菜id进行点菜++++++++++++++++++++++++++++++++++++++++++++++"); { OrderDishes od = new OrderDishes(); od.Show(); Console.WriteLine("按上述数字选择菜单,按e退出"); string sInKey = Console.ReadLine(); do { try { od.OrderDish(int.Parse(sInKey)); } catch (Exception ex) { LogHelper.WriteLog($" 报错信息:{ex.Message}"); } sInKey = Console.ReadLine(); } while (!sInKey.Equals("e", StringComparison.OrdinalIgnoreCase)); } } catch (Exception ex) { LogHelper.WriteLog(ex.Message); Console.ReadKey(); } #endregion #endregion }