Exemplo n.º 1
0
        static void Main(string[] args)
        {
            ShapeFactory shapeFactory = new ShapeFactory();

            IShape shape1 = shapeFactory.GetShape(ShapeFactory.ShapeType.StCircle);

            shape1.Draw();

            IShape shape2 = shapeFactory.GetShape(ShapeFactory.ShapeType.StRectangle);

            shape2.Draw();

            IShape shape3 = shapeFactory.GetShape(ShapeFactory.ShapeType.StSquare);

            shape3.Draw();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            ShapeFactory shapeFactory = new ShapeFactory();
            IShape       shape1       = shapeFactory.getShape("CIRCLE");

            shape1.Draw();

            IShape shape2 = shapeFactory.getShape("SQUARE");

            shape2.Draw();

            IShape shape3 = shapeFactory.getShape("RECTANGLE");

            shape3.Draw();

            Console.ReadKey();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            ShapeFactory shapeFactory = new ShapeFactory();

            IShape shape1 = shapeFactory.getSharp("Circle");

            shape1.draw();

            IShape shape2 = shapeFactory.getSharp("Rectangle");

            shape2.draw();

            IShape shape3 = shapeFactory.getSharp("Square");

            shape3.draw();

            Console.Read();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        public static void Main(String[] args)
        {
            ShapeFactory shapeFactory = new ShapeFactory();     //使用该工厂,通过传递类型信息来获取实体类的对象。

            IShape shape1 = shapeFactory.GetShape("CIRCLE");    //获取 Circle 的对象,并调用它的 draw 方法

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

            IShape shape2 = shapeFactory.GetShape("RECTANGLE"); //获取 Rectangle 的对象,并调用它的 draw 方法

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

            IShape shape3 = shapeFactory.GetShape("SQUARE");    //获取 Square 的对象,并调用它的 draw 方法

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

            Console.ReadLine();
        }
Exemplo n.º 5
0
        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        public static void Main(string[] args)
        {
            var shapeFactory = new ShapeFactory();

            // get an object of rectangle and call its draw method
            var rectangleShape = shapeFactory.GetShape("RECTANGLE");

            rectangleShape.Draw();

            // get an object of square and call its draw method
            var squareShape = shapeFactory.GetShape("SQUARE");

            squareShape.Draw();

            // get an object of circle and call its draw method
            var circleShape = shapeFactory.GetShape("CIRCLE");

            circleShape.Draw();

            Console.ReadKey();
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            ShapeFactory factory = new ShapeFactory();

            Shape[] shape     = new Shape[10];
            Random  random    = new Random();
            double  TotalArea = 0;

            for (int i = 0; i < 10; i++)
            {
                shape[i] = factory.GetShape(random.Next(0, 3).ToString());
                shape[i].Draw();
                shape[i].Display();
                shape[i].IsLegitimate();
                Console.WriteLine("面积为{0:0.0}", shape[i].GetArea());
                Console.WriteLine("");
                TotalArea += shape[i].GetArea();
            }
            Console.WriteLine("创建的图形的总面积为: {0:0.00}", TotalArea);

            Console.ReadKey();
        }
Exemplo n.º 7
0
        //https://www.tutorialspoint.com/design_pattern/factory_pattern.htm
        static void Main(string[] args)
        {
            ShapeFactory shapeFactory = new ShapeFactory();

            //get an object of Circle and call its draw method.
            Shape shape1 = shapeFactory.getShape("CIRCLE");

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

            //get an object of Rectangle and call its draw method.
            Shape shape2 = shapeFactory.getShape("RECTANGLE");

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

            //get an object of Square and call its draw method.
            Shape shape3 = shapeFactory.getShape("SQUARE");

            //call draw method of square
            shape3.draw();
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            var shapeFactory = new ShapeFactory();
            //获取 Circle 的对象,并调用它的 draw 方法
            var shape1 = shapeFactory.GetShape("CIRCLE");

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

            //获取 Rectangle 的对象,并调用它的 draw 方法
            var shape2 = shapeFactory.GetShape("RECTANGLE");

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

            //获取 Square 的对象,并调用它的 draw 方法
            var shape3 = shapeFactory.GetShape("SQUARE");

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

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            ShapeFactory shapeFactory = new ShapeFactory();

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

            shape1.Draw();

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

            shape2.Draw();

            try
            {
                IShape shape3 = shapeFactory.GetShape("Circle");
                shape3.Draw();
            }
            catch (NullReferenceException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Exception received.");
            }
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            ShapeFactory shapeFactory = new ShapeFactory();

            //获取 Circle 的对象,并调用它的 draw 方法
            Shape shape1 = shapeFactory.getShape("FactoryPattern.Circle");

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

            //获取 Rectangle 的对象,并调用它的 draw 方法
            Shape shape2 = shapeFactory.getShape("FactoryPattern.Rectangle");

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

            //获取 Square 的对象,并调用它的 draw 方法
            Shape shape3 = shapeFactory.getShape("FactoryPattern.Square");

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

            Console.ReadKey();
        }