开发者

A correct hierarchy for a Shape model

If I had to create a OOP model for a geometric Shape hierarchy which would be the best considering al开发者_运维技巧so a Point class?

Thanks.


What about using java.awt.Shape ?


If you want to completely encapsulate the idea of a shape, it should not be a hierarchy, as there are an infinite number of shapes in the universe. Instead, it should just be one Shape class. It should consist of a series of lines and curves. Then, you can have methods to check if it conforms to particular shape definitions, e.g. IsCircle(), IsSquare() etc...

You could also have methods and/or constructors that set it to a particular type of shape.


All shapes can probably implement an IShape interface. IShape would require an Area() method, and a IsIntersectingWith(IShape otherShape) method. This simplistic view though raises some other questions about how you will implement dynamic dispatch (triangle/triangle intersection requires a different algorithm from triangle/segment).

This of course is assuming you actually need this functionality. Assuming a graphics oriented shape library, you can define transformations like Rotate, Translate, Scale. You could also enforce the use of Union, Intersect or whatever other set operation (but those two can express everything you might need).

You can also base things towards computational geometry and provide method functions that convert any IShape into a Polygon.

My point is, the functionality that can be enforced through an IShape should follow the expected use. If you don't have any expected use (since its educational to start with) you should make the use as part of the exercise itself, or as a discovery field (how about this use, or that use, can we express all this functionality in one interface or should we split it into many)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜