Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
0
        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();
        }
        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);
        }
Exemplo n.º 6
0
        private void button1_Click(object sender, EventArgs e)
        {
            // 做西红柿炒蛋
            Food food1 = FoodSimpleFactory.CreateFood("西红柿炒蛋");

            food1.Print();

            Food food2 = FoodSimpleFactory.CreateFood("土豆肉丝");

            food1.Print();

            Console.Read();
        }
Exemplo n.º 7
0
        public IActionResult Index()
        {
            Food food1 = FoodSimpleFactory.CreateFood("西红柿炒鸡蛋");

            food1.Cook();

            Food food2 = FoodSimpleFactory.CreateFood("土豆丝");

            food2.Cook();


            return(View());
        }
Exemplo n.º 8
0
    static void __Main(string[] args)
    {
        // 客户想点一个西红柿炒蛋
        Food food1 = FoodSimpleFactory.CreateFood("西红柿炒蛋");

        food1.Print();

        // 客户想点一个土豆肉丝
        Food food2 = FoodSimpleFactory.CreateFood("土豆肉丝");

        food2.Print();

        Console.Read();
    }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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
        }