简体   繁体   English

如何设计此类的层次结构?

[英]How to design this class hierarchy?

I have defined an Event class: 我定义了一个Event类:

Event

and all the following classes inherit from Event: 并且以下所有类都继承自Event:

AEvent BEvent CEvent DEvent

Now, with the info I gather from all these Event classes, I will make a chart. 现在,利用我从所有这些Event类中收集的信息,我将制作一个图表。 With AEvent and BEvent, I will generate points for that chart, while with CEvent and DEvent I will paint certain regions of the chart. 使用AEvent和BEvent,我将为该图表生成点,而使用CEvent和DEvent,我将绘制图表的某些区域。

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? 我应该让AEvent和BEvent从PointEvent继承而CEvent和DEvent从RegionEvent继承,因为RegionEvent和PointEvent都从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. 谢谢。

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. 因此,我倾向于使用子类PointEventPainter和RegionEventPainter创建类似EventPainter的东西。 Some other entity would be responsible for getting the appropriate EventPainter for a given Event. 其他一些实体将负责获取给定事件的适当EventPainter。

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. 因为从本质上讲,AEvent和BEvent都是PointEvent子类,所以具有PointEvent类是有意义的。 Same is true with the RegionEvent. RegionEvent也是如此。

This will help you consolidate shared code, and avoid repeating code unnecessarily. 这将帮助您合并共享代码,并避免不必要地重复代码。

It really depends on your exact scenario. 这实际上取决于您的实际情况。 AEvent, BEvent, RegionEvent, PointEvent are a little vague. AEvent,BEvent,RegionEvent,PointEvent有点模糊。 ;-) ;-)

Option 1 seems to be ok in most cases. 在大多数情况下,选项1似乎还可以。 However it also sounds a bit like interfaces á la IDrawsRegion or IDrawsPoint would be good here. 但是,这听起来也有点像接口,在这里IDrawsRegionIDrawsPoint会很好。 Also I feel that a bit of the Strategy pattern shines through here which you might want to take a look at. 我也觉得,这里有些策略性的图案可以照耀您,您可能想看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM