jeudi 11 avril 2019

Access problems using the Factory Pattern on C#

I am very newbie with patterns, and I am starting to study the Factory Pattern to do an exercise about Geometric Figures.

This is my code for this case:

namespace MyNameSpace
{
    class MainApp
    {
        static void Main(string[] args)
        {
            //Problem: I can't access to 
            //the Cube() builder because it's private.
            GeometricFigure cube = new Cube(); 

            Console.WriteLine("Blablabla");
            Console.ReadLine();

        }
    }
}

This is my "Creator" method that implements the Factory pattern

public abstract class GeometricFigure
{
    private float Dimension { get; set; }
    private float Coordinate_X { get; set; }
    private float Coordinate_Y { get; set; }
    private float Coordinate_Z { get; set; }

    public GeometricFigure()
    {
        CreateFigure();
    }

    //Factory method
    public abstract void CreateFigure();
}

And this is my "ConcrateCreator" class

class Cube : GeometricFigure
{
    public override void CreateFigure() 
    {
        new Cube(); 
    }
}

I think that my code is good.... but... I can't understand why the Cube class is private. To sum up, Is it correct set to public the Cube Class? and if the answwer is not... How can I instance the cube object from the Main method if the Cube class it's private? :(

Thanks in advance to everybody.

Emma.

Aucun commentaire:

Enregistrer un commentaire