Sign in to follow this  

Trying to improve my design [C#]

This topic is 3312 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi folks, I'm working on a little GDI+ app in C# to be able to draw and edit vector graphics. Currently, I've got an inheritance hierarchy which basically amounts to a baseclass called Shape, and some subclasses called Rectangle, Line, Ellipse etc. The Shape class contains a load of protected variables common to the whole hierarchy; the shape's origin point; the Pen object passed in via the constructor; getters and setters etc. I was trying to keep the shape's drawing ability abstracted away as in a shape shouldn't know how to draw itself, but the solution to this involved lots of different classes that were actually quite specific e.g. a Rectangle needs to have an instance of drawRectangle which handles the drawing of a rectangle, similarly an Ellipse needs a drawEllipse... you get the picture, the number of classes went through the roof and it's really not terribly flexible. So, I decided to use an interface instead but, there's a bit of an issue I want to ask you about in terms of the design - it's worth mentioning the code I have runs and it runs perfectly. But here's what it is, in effect:
public abstract class Shape : IShapeDraw
{
// various protected members, virtual void()s etc.

// Then we get to the kicker - the interface members are implemented in Shape as virtual, and are then overridden by the subclasses underneath
}



It took me two weeks to realise that I was doing something completely and utterly dumb; why use the interface if you're just overriding it in the subclass as you normally would? Doesn't that make the interface unnecessary as you can just declare the same method as virtual in the superclass without ever writing the damned thing? The interface is only being implemented at the superclass level and not in any other class hierarchy. Is this good practice and does it need changing at all? It wouldn't take much to take it out if it's unnecessary but it does seem to me to be rather dumb and I don't want to go back to the old way of a bazillion drawFoo classes, high maintenance and combinatorial explosion. And is it wrong for a Shape or subtype of Shape to have explicit code within itself to draw itself, or should the drawing be abstracted out? What do you think?

Share this post


Link to post
Share on other sites
I think that something needs to know how to draw the thing. If it's a strategy or an explicit override is the focal point, and perhaps not so important in this case.

Share this post


Link to post
Share on other sites
No matter how much you abstract your design eventually you're going to have to program how to draw each shape. An easy way to do that would be to have an abstract method draw(Pen p) defined in Shape that each subclass can override with instructions on how to draw itself. Is there any reason you can't let a shape know how to draw itself?

Share this post


Link to post
Share on other sites

This topic is 3312 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this