devoured elysium
devoured elysium

Reputation: 105227

How to design this class hierarchy?

I have defined an Event class:

Event

and all the following classes inherit from Event:

AEvent BEvent CEvent DEvent

Now, with the info I gather from all these Event classes, I will make a chart. With AEvent and BEvent, I will generate points for that chart, while with CEvent and DEvent I will paint certain regions of the chart.

Now, how should I signal this in my class hierarchy?

  1. Should I make AEvent and BEvent inherit from PointEvent while CEvent and DEvent inherit from RegionEvent, being that both RegionEvent and PointEvent inherit from Event?
  2. Should I add a field with an Enum to Event with 2 values, Point and Region, and each of the child classes set their value to it?
  3. Should I use some kind of pattern here? Which one?

Thanks.

Upvotes: 0

Views: 316

Answers (3)

Carl Manaster
Carl Manaster

Reputation: 40356

I wouldn't think that an Event would have anything to do with drawing. So I would tend to create something like an EventPainter with subclasses PointEventPainter and RegionEventPainter. Some other entity would be responsible for getting the appropriate EventPainter for a given Event.

Upvotes: 2

herzmeister
herzmeister

Reputation: 11297

It really depends on your exact scenario. AEvent, BEvent, RegionEvent, PointEvent are a little vague. ;-)

Option 1 seems to be ok in most cases. However it also sounds a bit like interfaces á la IDrawsRegion or IDrawsPoint would be good here. Also I feel that a bit of the Strategy pattern shines through here which you might want to take a look at.

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564931

There isn't enough information to know for sure, but it sounds like your first option is the best.

Since AEvent and BEvent are both, in essense, PointEvent subclasses, it makes sense to have a PointEvent class. Same is true with the RegionEvent.

This will help you consolidate shared code, and avoid repeating code unnecessarily.

Upvotes: 1

Related Questions