Example #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;
            }
        }
        static void Main(string[] args)
        {
            AbstractFactory shapeFactory = FactoryProducer.GetFactory("shape");
            IShape          shapeCirle   = shapeFactory.GetShape("circle");

            shapeCirle.Draw();
            IShape shapeRectangle = shapeFactory.GetShape("rectangle");

            shapeRectangle.Draw();
            IShape shapeSquare = shapeFactory.GetShape("square");

            shapeSquare.Draw();

            AbstractFactory colorFactory = FactoryProducer.GetFactory("color");
            IColor          colorRed     = colorFactory.GetColor("red");

            colorRed.Fill();
            IColor colorGreen = colorFactory.GetColor("green");

            colorGreen.Fill();
            IColor colorBlue = colorFactory.GetColor("blue");

            colorBlue.Fill();

            Console.Read();
        }
Example #3
0
        static void Main(string[] args)
        {
            AbstractFactory shapeFactory = ProducterFactory.GetFactory("Shape");

            IShape shape1 = shapeFactory.GetShape("Shape1");

            shape1.Draw();

            IShape shape2 = shapeFactory.GetShape("Shape2");

            shape2.Draw();

            IShape shape3 = shapeFactory.GetShape("Shape3");

            shape3.Draw();

            AbstractFactory colorFactory = ProducterFactory.GetFactory("Color");

            IColor color1 = colorFactory.GetColor("Color1");

            color1.Fill();

            IColor color2 = colorFactory.GetColor("Color2");

            color2.Fill();

            IColor color3 = colorFactory.GetColor("Color3");

            color3.Fill();

            Console.ReadKey();
        }
Example #4
0
        public void CreateTest()
        {
            IShape shape = clientFactory.GetShape();
            IColor color = clientFactory.GetColor();

            shape.Draw();
            color.Fill();
            Console.WriteLine("Test generated");
        }
        /// <summary>
        /// Creational Design Pattern
        /// </summary>
        /// <param name="args">Abstract Factory Pattern</param>


        /*
         * Abstract Factory patterns work around a super-factory which creates other factories.
         * This factory is also called as factory of factories.
         *
         * Interface is responsible for creating a factory of related objects without explicitly specifying their classes.
         * Each generated factory can give the objects as per the Factory pattern.
         */

        static void Main(string[] args)
        {
            //get shape factory
            AbstractFactory shapeFactory = FactoryProducer.GetFactory("SHAPE");

            //get color factory
            AbstractFactory colorFactory = FactoryProducer.GetFactory("COLOR");


            if (shapeFactory != null && colorFactory != null)
            {
                //get an object of Shape Circle
                IShape shape1 = shapeFactory.GetShape("CIRCLE");

                //call draw method of Shape Circle
                shape1.Draw();

                //get an object of Shape Rectangle
                IShape shape2 = shapeFactory.GetShape("RECTANGLE");

                //call draw method of Shape Rectangle
                shape2.Draw();

                //get an object of Shape Square
                IShape shape3 = shapeFactory.GetShape("SQUARE");

                //call draw method of Shape Square
                shape3.Draw();

                Console.WriteLine("\n");

                //get an object of Color Red
                IColor color1 = colorFactory.GetColor("RED");

                //call fill method of Red
                color1.Fill();

                //get an object of Color Green
                IColor color2 = colorFactory.GetColor("GREEN");

                //call fill method of Green
                color2.Fill();

                //get an object of Color Blue
                IColor color3 = colorFactory.GetColor("BLUE");

                //call fill method of Color Blue
                color3.Fill();
            }
            else
            {
                Console.WriteLine("Input is invalid. Please give input Shape or Color option.");
            }

            Console.ReadLine();
        }
Example #6
0
        static void Main(string[] args)
        {
            AbstractFactory shape    = FactoryProducer.GetFactory("SHAPE");
            IShape          shapeObj = shape.GetShape("SQUARE");

            shapeObj.Draw();
            shapeObj = shape.GetShape("CIRCLE");
            shapeObj.Draw();
            shapeObj = shape.GetShape("RECTANGLE");
            shapeObj.Draw();

            AbstractFactory color    = FactoryProducer.GetFactory("COLOR");
            IColor          colorObj = color.GetColor("RED");

            colorObj.Fill();
            colorObj = color.GetColor("GREEN");
            colorObj.Fill();
            colorObj = color.GetColor("BLUE");
            colorObj.Fill();
        }
        public void TestAbstractFactory()
        {
            AbstractFactory _factory = FactoryProducer.GetFactory("SHAPE");
            IShape          shp      = _factory.GetShape("RECTANGLE");

            shp.Draw();

            _factory = FactoryProducer.GetFactory("COLOR");
            IColor clr = _factory.GetColor("RED");

            clr.Fill();
        }
        public void Run()
        {
            System.Console.WriteLine("I am Abstract Faction Pattern");

            AbstractFactory shapeFactory = FactoryProducer.GetFactory("Shape");
            IShape          shape        = shapeFactory.GetShape("Circle");

            shape.Draw();
            shape = shapeFactory.GetShape("Rectangle");
            shape.Draw();
            shape = shapeFactory.GetShape("Square");
            shape.Draw();

            AbstractFactory colorFactory = FactoryProducer.GetFactory("Color");
            IColor          color        = colorFactory.GetColor("Red");

            color.Fill();
            color = colorFactory.GetColor("Green");
            color.Fill();
            color = colorFactory.GetColor("Blue");
            color.Fill();
        }
Example #9
0
        static void Main(string[] args)
        {
            Factories.AbstractFactory shapeFactory = FactoryProducer.GetFactory(ProducerType.Shape);
            IShape shape = shapeFactory.GetShape(ShapeType.Circle);

            shape.Draw();

            Factories.AbstractFactory colorFactory = FactoryProducer.GetFactory(ProducerType.Color);
            IColor color = colorFactory.GetColor(ColorType.Red);

            color.Fill();

            Console.ReadKey();
        }
        public static void Main(string[] args)
        {
            //获取形状工厂
            AbstractFactory shapeFactory = FactoryProducer.GetFactory("Shape");

            //获取形状为 Circle 的对象
            IShape shape1 = shapeFactory.GetShape("Circle");

            //调用 Circle 的 draw 方法
            shape1.Draw();

            //获取形状为 Rectangle 的对象
            IShape shape2 = shapeFactory.GetShape("Rectangle");

            //调用 Rectangle 的 draw 方法
            shape2.Draw();

            //获取形状为 Square 的对象
            IShape shape3 = shapeFactory.GetShape("Square");

            //调用 Square 的 draw 方法
            shape3.Draw();

            //获取颜色工厂
            AbstractFactory colorFactory = FactoryProducer.GetFactory("Color");

            //获取颜色为 Red 的对象
            IColor color1 = colorFactory.GetColor("Red");

            //调用 Red 的 fill 方法
            color1.Fill();

            //获取颜色为 Green 的对象
            IColor color2 = colorFactory.GetColor("Green");

            //调用 Green 的 fill 方法
            color2.Fill();

            //获取颜色为 Blue 的对象
            IColor color3 = colorFactory.GetColor("Blue");

            //调用 Blue 的 fill 方法
            color3.Fill();

            Console.ReadLine();
        }
Example #11
0
        static void Main(string[] args)
        {
            //获取形状工厂
            AbstractFactory shapeFactory = FactoryProducer.GetFactory("SHAPE");

            //获取形状为 Circle 的对象
            IShape shape1 = shapeFactory.GetShape("CIRCLE");

            //调用 Circle 的 draw 方法
            shape1.Draw();

            //获取形状为 Rectangle 的对象
            IShape shape2 = shapeFactory.GetShape("RECTANGLE");

            //调用 Rectangle 的 draw 方法
            shape2.Draw();

            //获取形状为 Square 的对象
            IShape shape3 = shapeFactory.GetShape("SQUARE");

            //调用 Square 的 draw 方法
            shape3.Draw();

            //获取颜色工厂
            AbstractFactory colorFactory = FactoryProducer.GetFactory("COLOR");

            //获取颜色为 Red 的对象
            IColor color1 = colorFactory.GetColor("RED");

            //调用 Red 的 fill 方法
            color1.Fill();

            //获取颜色为 Green 的对象
            IColor color2 = colorFactory.GetColor("GREEN");

            //调用 Green 的 fill 方法
            color2.Fill();

            //获取颜色为 Blue 的对象
            IColor color3 = colorFactory.GetColor("BLUE");

            //调用 Blue 的 fill 方法
            color3.Fill();

            Console.ReadKey();
        }
Example #12
0
        private void btAbstractFactory_Click(object sender, EventArgs e)
        {
            string str = "";
            //获取形状工厂
            IAbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
            //获取形状为 Circle 的对象
            IShape shape1 = shapeFactory.getShape("CIRCLE");

            //调用 Circle 的 draw 方法
            str += shape1.draw();

            //获取形状为 Rectangle 的对象
            IShape shape2 = shapeFactory.getShape("RECTANGLE");

            //调用 Rectangle 的 draw 方法
            str += shape2.draw();

            //获取形状为 Square 的对象
            IShape shape3 = shapeFactory.getShape("SQUARE");

            //调用 Square 的 draw 方法
            str += shape3.draw();

            //获取颜色工厂
            IAbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

            //获取颜色为 Red 的对象
            IColor color1 = colorFactory.getColor("RED");

            //调用 Red 的 fill 方法
            str += color1.Fill();

            //获取颜色为 Green 的对象
            IColor color2 = colorFactory.getColor("GREEN");

            //调用 Green 的 fill 方法
            str += color2.Fill();

            //获取颜色为 Blue 的对象
            IColor color3 = colorFactory.getColor("BLUE");

            //调用 Blue 的 fill 方法
            str += color3.Fill();
            tbOutWindow.Text = str;
        }
Example #13
0
        static void Main(string[] args)
        {
            AbstractFactory mRedCircle = new RedCircle();
            IShape          shape1     = mRedCircle.GetShape();

            shape1.Draw();
            IColor color1 = mRedCircle.GetColor();

            color1.Fill();

            AbstractFactory mBlueSquare = new BlueSquare();
            IShape          shape2      = mBlueSquare.GetShape();

            shape2.Draw();
            IColor color2 = mBlueSquare.GetColor();

            color2.Fill();
        }
Example #14
0
        public static void DoMain(String[] args)
        {
            //获取形状工厂
            AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

            //获取形状为 Circle 的对象
            IShape shape1 = shapeFactory.getShape("CIRCLE");

            //调用 Circle 的 Draw 方法
            shape1.Draw();

            //获取形状为 Rectangle 的对象
            IShape shape2 = shapeFactory.getShape("RECTANGLE");

            //调用 Rectangle 的 Draw 方法
            shape2.Draw();

            //获取形状为 Square 的对象
            IShape shape3 = shapeFactory.getShape("SQUARE");

            //调用 Square 的 Draw 方法
            shape3.Draw();

            //获取颜色工厂
            AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

            //获取颜色为 Red 的对象
            IColor color1 = colorFactory.getColor("RED");

            //调用 Red 的 Fill 方法
            color1.Fill();

            //获取颜色为 Green 的对象
            IColor color2 = colorFactory.getColor("Green");

            //调用 Green 的 Fill 方法
            color2.Fill();

            //获取颜色为 Blue 的对象
            IColor color3 = colorFactory.getColor("BLUE");

            //调用 Blue 的 Fill 方法
            color3.Fill();
        }
Example #15
0
        static void TestPaint()
        {
            //get IShape factory
            PaintAbstractFactory shapeFactory = PaintFactoryProducer.GetPaintFactory("Shape");

            //get an object of IShape Circle
            IShape shape1 = shapeFactory.CreateShape("CIRCLE");

            //call Draw method of IShape Circle
            shape1.Draw();

            //get an object of IShape Rectangle
            IShape shape2 = shapeFactory.CreateShape("RECTANGLE");

            //call Draw method of IShape Rectangle
            shape2.Draw();

            //get an object of IShape Square
            IShape shape3 = shapeFactory.CreateShape("SQUARE");

            //call Draw method of IShape Square
            shape3.Draw();

            //get color factory
            PaintAbstractFactory colorFactory = PaintFactoryProducer.GetPaintFactory("COLOR");

            //get an object of Color Red
            IColor color1 = colorFactory.CreateColor("RED");

            //call fill method of Red
            color1.Fill();

            //get an object of Color Green
            IColor color2 = colorFactory.CreateColor("Green");

            //call fill method of Green
            color2.Fill();

            //get an object of Color Blue
            IColor color3 = colorFactory.CreateColor("BLUE");

            //call fill method of Color Blue
            color3.Fill();
        }
Example #16
0
        public static void Demo()
        {
            AbstractFactory shapeGen = FactoryProducer.Generator("Shape");

            Factory.IShape c = shapeGen.ShapeCreate("Circle");
            c.Draw();
            Factory.IShape s = shapeGen.ShapeCreate("Square");
            s.Draw();

            AbstractFactory colorGen = FactoryProducer.Generator("Color");
            IColor          r        = colorGen.ColorCreate("Red");

            r.Fill();
            IColor b = colorGen.ColorCreate("Blue");

            b.Fill();

            Console.ReadLine();
        }
Example #17
0
        public static void GetAbstractFactory()
        {
            AbstractFactory shapeFactory = FactoryProducer.getFactory("Shape");
            IShape          shape1       = shapeFactory.getShape("Circle");
            IShape          shape2       = shapeFactory.getShape("Square");
            IShape          shape3       = shapeFactory.getShape("Rectangle");

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

            AbstractFactory colorFactory = FactoryProducer.getFactory("Color");
            IColor          color1       = colorFactory.getColor("Red");
            IColor          color2       = colorFactory.getColor("Blue");
            IColor          color3       = colorFactory.getColor("Green");

            color1.Fill();
            color2.Fill();
            color3.Fill();
        }
Example #18
0
        static void Main(string[] args)
        {
            FactoryProducer Fproducer    = new FactoryProducer();
            AbstractFactory shapeFactory = Fproducer.GetFactory("Shape");
            IShape          Circle       = shapeFactory.GetShape(shape.Circle);

            Circle.Draw(10);
            IShape Square = shapeFactory.GetShape(shape.Square);

            Square.Draw(20);

            IColor temp = shapeFactory.GetColor("");

            AbstractFactory colorFactory = Fproducer.GetFactory("Color");
            IColor          Red          = colorFactory.GetColor("Red");
            IColor          Blue         = colorFactory.GetColor("Blue");

            Red.Fill("");
            Blue.Fill("");
            Console.ReadKey();
        }
Example #19
0
        static void Main(string[] args)
        {
            Factory shapeFact = Producer.GetFactory("shape");

            IShape circle = shapeFact.GetShape("circle");

            circle.Draw();

            IShape rectangle = shapeFact.GetShape("rectangle");

            rectangle.Draw();

            Factory colorFactory = Producer.GetFactory("color");

            IColor blueColor = colorFactory.GetColor("blue");

            blueColor.Fill();

            IColor redColor = colorFactory.GetColor("red");

            redColor.Fill();

            Console.ReadKey();
        }
Example #20
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"));
        }
Example #21
0
 public void GetShapeAndColor()
 {
     _shape.Draw();
     _color.Fill();
 }