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

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

我正在尝试学习 C#,在 C# 中,我正在尝试实现一个工厂设计模式来绘制基本形状,如圆形、矩形、三角形等。我创建了一个 IShape 接口 class .以及其他三个圆形、矩形和三角形 class。我还创建了一个 ShapeFactory 静态方法。这些 classes 应该在编译器 class 之后调用,并在另一个 window 中绘制,即输出 window。下面是代码。

//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";
    }
}

在编译器 class 中,我想通过 outputWindow.show(); 打开输出 window,然后通过 outputWindow.OutputArea.Image=...[=15 在 window 中绘制形状=]

我卡住了。请帮忙。

This post 解释了

之间的区别
  • 工厂,
  • 工厂方法,
  • 抽象工厂

设计模式。

由于您希望能够创建需要 "the same kind" 的整个对象系列,这可以通过 抽象工厂 模式来完成。

工厂可以有多个创建者方法。您可以向工厂的创建者方法添加参数。

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);

}

工厂的实现会处理将 Graphics 对象传递给它创建的形状。

ShapeFactory.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]
}

在用工厂:

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