Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("选择工厂模式");
            var type = Console.ReadLine();

            switch (type)
            {
            case "1":    //工厂模式
                var   sharpFactory = new SharpeFactory();
                Sharp sharp        = sharpFactory.GetSharpe("R");
                sharp.Draw();
                Console.ReadLine();
                break;

            case "2":    //抽象工厂模式
                AbstractFactory abstractFactory = FactoryProducer.GetFactory("1212");
                IColor          color           = abstractFactory.GetColor("B");
                color.Fill();
                Console.ReadLine();
                break;

            case "3":    //建造者模式
                MealBuilder mealBuilder = new MealBuilder();
                Meal        vgMeal      = mealBuilder.PrepareVegMeal();
                Console.WriteLine("Veg meal");
                vgMeal.ShowItems();
                Console.WriteLine($"Total Cost:{vgMeal.GetCost()}");
                Meal nonVegMeal = mealBuilder.PrepareNonVegMeal();
                Console.WriteLine("NonVeg Meal");
                nonVegMeal.ShowItems();
                Console.WriteLine($"Total Cost:{nonVegMeal.GetCost()}");
                break;
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            #region Builder Pattern
            Console.WriteLine("-------Builder Pattern--------");
            MealBuilder mb = new MealBuilder();

            Meal Vegan = mb.PrepareVegMeal();
            Console.WriteLine("Preparing...Vegan");
            Vegan.ShowItems();
            Console.WriteLine("Cost:{0}", Vegan.GetCost());


            MealBuilder mbu = new MealBuilder();

            Meal HardCode = mb.PrepareNonVegMeal();
            Console.WriteLine("Preparing...Hard");
            Vegan.ShowItems();
            Console.WriteLine("Cost:{0}", HardCode.GetCost());
            #endregion
            Console.WriteLine("---------------------------");


            Console.WriteLine("-------ProtoType Pattern---------");
            ShapeCache.LoadCache();

            ProtoProj.Shape clonedShape = (ProtoProj.Shape)ShapeCache.GetShape("1");
            Console.WriteLine("Shape: {0}", clonedShape.GetType());

            ProtoProj.Shape clonedShape1 = (ProtoProj.Shape)ShapeCache.GetShape("2");
            Console.WriteLine("Shape: {0}", clonedShape1.GetType());

            ProtoProj.Shape clonedShape2 = (ProtoProj.Shape)ShapeCache.GetShape("3");
            Console.WriteLine("Shape: {0}", clonedShape2.GetType());


            Console.WriteLine("-------Adapter Pattern------");

            AudioPlayer ap = new AudioPlayer();
            ap.Play("mp4", "alone.mp4");
            ap.Play("mp3", "more_than_words.mp3");
            ap.Play("vlx", "beyond_horizon.vlc");

            Console.WriteLine("-----Bridge Pattern------");

            BridgeProj.Shape red   = new BridgeProj.Circle(new RedCircle(), 100, 100, 100);
            BridgeProj.Shape green = new BridgeProj.Circle(new GreenCircle(), 100, 100, 100);

            red.Draw();
            green.Draw();

            Console.WriteLine("---------------------------");



            Console.ReadKey();
        }
Esempio n. 3
0
        public void Start()
        {
            var mealBuilder = new MealBuilder();
            var vegMeal     = mealBuilder.PrepareVegMeal();

            logger.Info($"Veg Meal: ");
            vegMeal.ShowItems();

            var nonVegMeal = mealBuilder.PrepareNonVegMeal();

            logger.Info($"Non Veg Meal: ");
            nonVegMeal.ShowItems();
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            MealBuilder vegMealBuilder = new MealBuilder();
            Meal        vegMeal        = vegMealBuilder.PrepareVegMeal();

            vegMeal.ShowMealItems();
            Console.WriteLine($"\nTotal Price : Rs.{vegMeal.GetFinalCost()}/-");
            Meal nonVegMeal = vegMealBuilder.PrepareNonVegMeal();

            nonVegMeal.ShowMealItems();
            Console.WriteLine($"\nTotal Price : Rs.{nonVegMeal.GetFinalCost()}/-");
            Console.ReadLine();
        }
Esempio n. 5
0
        static void TestFoods()
        {
            MealBuilder mealBuilder = new MealBuilder();
            Meal        vegMeal     = mealBuilder.PrepareVegMeal();

            vegMeal.ShowItems();

            Debug.WriteLine($"total price: {vegMeal.CalculateCost()}");

            Meal nonVegMeal = mealBuilder.PrepareNonVegMeal();

            nonVegMeal.ShowItems();
            Debug.WriteLine($"total price: {nonVegMeal.CalculateCost()}");
        }
        static void Main(string[] args)
        {
            MealBuilder mealBuilder = new MealBuilder();

            Meal vegMeal = mealBuilder.PrepareVegMeal();

            Console.WriteLine("Vegetable Meal -> ");
            vegMeal.ShowItems();
            Console.WriteLine("Total Cost: " + vegMeal.GetCost());

            Meal nonVegMeal = mealBuilder.PrepareNonVegMeal();

            Console.WriteLine("Non-Vegetable Meal -> ");
            nonVegMeal.ShowItems();
            Console.WriteLine("Total Cost: " + nonVegMeal.GetCost());
        }
        public void Builder_Pattern_Test()
        {
            var builder = new MealBuilder();

            var vegMeal  = builder.PrepareVegMeal();
            var vegItems = vegMeal.GetItems();

            Assert.IsInstanceOfType(vegItems[0], typeof(VegBurger));
            Assert.IsInstanceOfType(vegItems[1], typeof(Coke));


            var chickenMeal  = builder.PrepareNonVegMeal();
            var chickenItems = chickenMeal.GetItems();

            Assert.IsInstanceOfType(chickenItems[0], typeof(ChickenBurger));
            Assert.IsInstanceOfType(chickenItems[1], typeof(Pepsi));
        }
        public static void Start()
        {
            MealBuilder mealBuilder = new MealBuilder();

            Meal vegMeal = mealBuilder.PrepareVegMeal();

            Console.WriteLine("Veg Meal");
            vegMeal.ShowItems();
            Console.WriteLine($"Total Cost: {vegMeal.GetCost()}");


            Meal nonVegMeal = mealBuilder.PrepareNonVegMeal();

            Console.WriteLine("\n\nNon-Veg Meal");
            nonVegMeal.ShowItems();
            Console.WriteLine($"Total Cost: {nonVegMeal.GetCost()}");
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("Singleton Pattern");

            // Singleton Pattern
            SingleObject singleObject = SingleObject.Instance();

            singleObject.ShowMessage();

            Console.WriteLine("\nBuilder Pattern");

            // Builder Pattern
            MealBuilder mealBuilder = new MealBuilder();

            Meal vegMeal = mealBuilder.PrepareVegMeal();

            Console.WriteLine("Veg Meal");
            vegMeal.ShowItems();
            Console.WriteLine("Total Cost : " + vegMeal.GetCost());

            Meal nonVegMeal = mealBuilder.PrepareNonVegMeal();

            Console.WriteLine("NonVeg Meal");
            nonVegMeal.ShowItems();
            Console.WriteLine("Total Cost : " + nonVegMeal.GetCost());

            Console.WriteLine("\nAbstract Factory");

            // Abstract Factory Pattern
            AbstractFactory shapeFactory = FactoryProducer.GetFactory("shape");
            IShape          circleShape  = shapeFactory.GetShape("circle");

            circleShape.Draw();

            AbstractFactory colorFactory = FactoryProducer.GetFactory("color");
            IColor          blueColor    = colorFactory.GetColor("blue");

            blueColor.Fill();

            Console.WriteLine("\nAdapter");

            //Adapter Pattern
            AudioPlayer audioPlayer = new AudioPlayer();

            audioPlayer.Play("mp4");
            audioPlayer.Play("flac");
            audioPlayer.Play("vlc");

            Console.WriteLine("\nFacade");

            //Facade Pattern
            ShapeMaker shapeMaker = new ShapeMaker();

            shapeMaker.DrawCircle();
            shapeMaker.DrawRectangle();
            shapeMaker.DrawSquare();

            Console.WriteLine("\nDecorator doesn't work");

            //Decorator Pattern
            IDecoratorShape rectangle    = new RectangleDecorator();
            IDecoratorShape redRectangle = new RedShapeDecorator(new RectangleDecorator());

            Console.WriteLine("Rectangle with no color");
            rectangle.Draw();
            Console.WriteLine("Rectangle with color");
            redRectangle.Draw();

            Console.WriteLine("\nComposite");

            //Composite Pattern
            Employee DSI           = new Employee("employee1", "DSI", 100000);
            Employee chefDeProjet1 = new Employee("employee2", "Chef de projet", 60000);
            Employee chefDeProjet2 = new Employee("employee3", "Chef de projet", 60000);
            Employee dev1          = new Employee("employee4", "Développeur", 40000);
            Employee dev2          = new Employee("employee5", "Développeur", 40000);

            DSI.Add(chefDeProjet1);
            DSI.Add(chefDeProjet2);
            chefDeProjet1.Add(dev1);
            chefDeProjet2.Add(dev2);

            Console.WriteLine(DSI.Details());
            foreach (Employee e1 in DSI.GetSubordinates())
            {
                Console.WriteLine(e1.Details());
                foreach (Employee e2 in e1.GetSubordinates())
                {
                    Console.WriteLine(e2.Details());
                }
            }

            Console.WriteLine("\nBridge");

            //Bridge Pattern
            BridgeShape shape1 = new BridgeCircle(10, 10, 1, new GreenCircle());
            BridgeShape shape2 = new BridgeCircle(100, 100, 10, new RedCircle());

            shape1.Draw();
            shape2.Draw();

            Console.WriteLine("\nCommand");

            //Command Pattern
            Stock    stock1   = new Stock("laptop", 100);
            BuyStock buyStock = new BuyStock(stock1);

            Stock     stock2    = new Stock("screen", 30);
            SellStock sellStock = new SellStock(stock2);

            Broker broker = new Broker();

            broker.TakeOrder(buyStock);
            broker.TakeOrder(sellStock);
            broker.PlaceOrders();

            Console.WriteLine("\nInterpreter");

            //Interpreter Pattern
            IExpression isMale    = InterpreterPatternDemo.GetMaleExpression();
            IExpression isMarried = InterpreterPatternDemo.GetMarriedWomanExpression();

            Console.WriteLine("John is male ? " + isMale.Interpret("john"));
            Console.WriteLine("Barbara is male ? " + isMale.Interpret("barbara"));

            Console.WriteLine("Julie is married ? " + isMarried.Interpret("julie married"));
            Console.WriteLine("Bob is married ? " + isMarried.Interpret("bob married"));
        }