I am asking a question about design patterns. I have an IDrawing interface, and two classes called TextDrawing and ShapeDrawing that implement this interface. I also have a View class that knows how to draw these classes. However, I have more complex drawing classes that also implement the IDrawing interface, but are composed of several other IDrawing classes.
I am not sure how to draw these complex classes in my View class. I don't want to teach the View class how to draw each new IDrawing class because it would not be a good design. What other options do I have? Is my design incorrect, and if so, how can I tell the View's Draw method to know the primitive parts of the complex classes and draw them? Here is my code:
public interface IDrawing { } public class TextDrawing : IDrawing { } public class ShapeDrawing : IDrawing { } public class SignDrawing : IDrawing { public TextDrawing Text { get; set; } public ShapeDrawing Border { get; set; } } public class MoreComplexDrawing : IDrawing { public TextDrawing Text { get; set; } public ShapeDrawing Border1 { get; set; } public ShapeDrawing Border2 { get; set; } } public class View { public void Draw(IDrawing drawing) { // The View only knows how to draw TextDrawing and ShapeDrawing. // These as the primitive building blocks of all drawings. // How can it draw the more complex ones! if (drawing is TextDrawing) { // draw it } else if (drawing is ShapeDrawing) { // draw it } else { // extract the drawings primitive parts (TextDrawing and ShapeDrawing) and draw them! } } } Update:
I received suggestions to implement the Draw() method in my drawing classes, but if I do this, they will not be general anymore. For example, I will not be able to use them in other projects where I have a different strategy for drawing. Is there a way to make my design more flexible and maintain its generality?