mardi 22 août 2017

The correct way to change the base class from derived class

I'm trying to design several base drawing classes from which I can inherit and define complex drawings.

As you can see, LineDrawing and CompoundDrawing derive from an abstract Drawing class. The CompoundDrawing has an internal List<Drawing> which allows us to store as many Drawing objects as we want and defined more complex drawings.

public abstract class Drawing
{       
    public bool CanBeRotated { get; set; }

    private float m_rotation;
    public float Rotation
    {
        get { return m_rotation; }
    }

    protected Drawing()
    {
        CanBeRotated = true;
    }

    public void Rotate(float degree)
    {
        if (CanBeRotated)
            m_rotation = degree;
    }
}

public sealed class LineDrawing : Drawing
{
    private readonly Line m_line;

    public Line Line
    {
        get { return m_line; }
    }

    public LineDrawing(Line line)
    {
        m_line = line;
    }
}

public class CompoundDrawing : Drawing
{
    protected IList<Drawing> m_drawings;

    protected CompoundDrawing(IList<Drawing> drawings)
    {
        m_drawings = new List<Drawing>(drawings);
    }
}

Let's say I want to define a RectangleDrawing that derives from CompoundDrawing.

public class RectangleDrawing : CompoundDrawing
{
    public RectangleDrawing(IList<LineDrawing> lineDrawings) : base(lineDrawings)
    {
    }    
}

Now I face a problem here! Obviously, I do not want the LineDrawing objects in this new class to be Rotatable! But I'm not sure where in this pattern I should set that!

Aucun commentaire:

Enregistrer un commentaire