简体   繁体   English

如何在 c# 中实现工厂设计模式来绘制形状?

[英]how to implement factory design pattern in c# to draw shapes?

I am trying to learn c# and in c# I am trying to implement a factory design pattern to draw basic shapes like circle, rectangle, triangle, etc. I created a IShape Interface class.我正在尝试学习 c# 和 c# 我正在尝试实现工厂设计模式来绘制圆形、矩形、三角形等基本形状。我创建了一个IShape接口 class。 And other three Circle, Rectangle, and Triangle class.以及其他三个圆形、矩形和三角形 class。 I also created a ShapeFactory static method.我还创建了一个ShapeFactory static 方法。 These classes sould be called after from compiler class and draw in another window ie Output window.这些类应该在编译器 class 之后调用,并在另一个 window 中绘制,即 Output Z05B8C742CBD96FBF2DEZC1A354F Below is the code.下面是代码。

//IShape Class
namespace CPaint.Class
{
    public interface IShape
    {
        void Draw();
    }
}

//Cicle Class
namespace CPaint.Class
{
    internal class Circle : IShape
    {
        //public Output outputWindow;
        private float x;

        private float y;
        private Color color;
        private float radius;
        private Graphics graphics;

        public Circle(Color color, float x, float y, float radius)
        {
            this.color = color;
            this.x = x;
            this.y = y;
            this.radius = radius;
        }

        public void Draw()
        {
            Pen pen = new Pen(Color.Black, 4);
            SolidBrush brush = new SolidBrush(color);
            graphics.FillEllipse(brush, x, y, radius, radius);
            graphics.DrawEllipse(pen, x, y, radius, radius);
        }
    }
}

//Triangle class
namespace CPaint.Class
{
    internal class Triangle : IShape
    {
        private float x;
        private float y;
        private Color color;
        private float side1;
        private float side2;
        private float side3;

        public Triangle(Color color, float x, float y, float side1, float side2, float side3)
        {
            this.color = color;
            this.x = x;
            this.y = y;
            this.side1 = side1;
            this.side2 = side2;
            this.side3 = side3;
        }

        public void Draw()
        {
            //will write drawing code here...
        }
    }
}

//Shape Factory Class
namespace CPaint.Class
{
    public class ShapeFactory
    {
        public static IShape GetShape(string shapeType, Color color, float x, float y, float height, float width, float radius, float side1, float side2, float side3)
        {
            if (shapeType == null)
            {
                return null;
            }

            if (shapeType.Equals("circle"))
            {
                return new Circle(color, x, y, radius);
            }
            else if (shapeType.Equals("rectangle"))
            {
                return new Rectangle(color, x, y, height, width); ;
            }
            else if (shapeType.Equals("triangle"))
            {
                return new Triangle(color, x, y, side1, side2, side3);
            }
            else
            {
                return null;
            }
        }
    }
}

//Compiler Class
if (code[0].Trim().ToLower().Equals("circle"))
{
    try
    {
        int parameter1 = Int32.Parse(code[1]);

        IShape shape = ShapeFactory.GetShape("circle", color, initX, initY, 0, 0, parameter1, 0, 0, 0);
        outputWindow.Show();

        //outputWindow.outputArea.Image=new ShapeFactory.GetShape("circle", color, initX, initY, 0, 0, parameter1, 0, 0, 0);
        //outputWindow.outputArea.Image = shape;

        output = "command executed successfully: parameter is" + parameter1;
        //output = "is numeric ";
    }

    catch (Exception ex)
    {
        Console.WriteLine("Exception Message: " + ex.Message);
        output = "\n Parameter Error : Invalid/Insufficient Parameter. \n";
    }
}

Here in Compiler class, I want to open output window by outputWindow.show();在编译器 class 中,我想通过outputWindow.show(); and then draw the shape in window by outputWindow.OutputArea.Image=...然后通过outputWindow.OutputArea.Image=...在 window 中绘制形状

I got stuck.我被困。 Please help.请帮忙。

This post explains differences between 这篇文章解释了两者之间的区别

  • Factory,工厂,
  • Factory Method, and工厂方法和
  • Abstract Factory抽象工厂

design patterns.设计模式。

Since you want to be able to create a whole family of objects that need to be of "the same kind", this can be done with Abstract Factory pattern.由于您希望能够创建需要“同类”的整个对象系列,因此可以使用抽象工厂模式来完成。

The factory can have multiple creator methods.工厂可以有多个创建者方法。 You can add parameters to the creator method(s) of the factory.您可以向工厂的创建者方法添加参数。

IShapesFactory.cs is the interface of the Abstract Factory: IShapesFactory.cs 是抽象工厂的接口:

public interface IShapesFactory
{
    Circle MakeCircle(Color color, float x, float y, float radius);
    Triangle MakeTriangle(Color color, float x, float y, float side1, float side2, float side3);

}

The implementation of the factory would handle passing the Graphics object to the shapes it creates.工厂的实现将处理将Graphics object 传递给它创建的形状。

ShapeFactory.cs形状工厂.cs

public class ShapeFactory: IShapeFactory
{
    private Graphics _graphics;

    public ShapeFactory(Graphics graphics)
    {
        _graphics = graphics;
    }

    public Circle MakeCircle(Color color, float x, float y, float radius)
    {
        // pass the Graphics object in the constructor of Circle
        return new Circle(_graphics, color, x, y, radius); 
    }

    [..other methods of the factory interface]
}

The factory in use:使用工厂:

IShapeFactory factory = new ShapeFactory(graphics);
var circle = factory.MakeCircle(Color.Blue, 10, 10, 20);
circle.Draw();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM