简体   繁体   English

继承层次结构-如何拆分类

[英]Inheritance hierarchy - how to split class

i have a class which draws a graph for a particular entity , the graphs nodes are clickable and as such i have specific methods for the clicked item to show appropriate information. 我有一个为特定实体绘制图形的类,图形节点是可单击的,因此,我具有用于单击项的特定方法以显示适当的信息。

As this is just a prototype I haven't considered the other entities - which will have entirely different nodes to click on. 因为这只是一个原型,所以我没有考虑其他实体-要单击的实体将完全不同。

i am unsure how using inheritance i can split it up so i have a graph as a main class, and depending on what entity (type of graph is drawn) split up the relevant methods for clicked items so they are segregated in to their own space? 我不确定如何使用继承将其拆分,因此我将一个图作为主要类,并根据哪个实体(绘制的图类型)拆分了单击项的相关方法,以便将它们分离到各自的空间中?

For example, a graph of type 例如,一个类型的图

Bannana - has skin, size, colour, quantity as clickable entitys Bannana-具有可点击实体的皮肤,大小,颜色,数量

but a graph of type 但是一个类型的图

Orange - has colour, segments, type of orange, quantity 橙色-具有颜色,线段,橙色类型,数量

Any ideas, sorry for the poor example :/ 任何想法,对不起这个糟糕的例子:/

Thanks 谢谢

Below we have an xml file for one entity, based on the heirarchy of node types is how the tree is drawn. 根据节点类型的继承关系,下面是一个实体的xml文件,它是如何绘制树的。 The other entitys will be laid out similarly but will have different values. 其他实体的布局将类似,但具有不同的值。 Does this help clear things up? 这有助于清除一切吗? Also might be worth mentioning the different entities will share some of the same items, so for example all of them have a click to view compound image tag 还可能值得一提的是,不同的实体将共享一些相同的项目,因此,例如,所有这些实体都可以单击以查看复合图像标签

<graph>
  <node label="Batch">
    <node label="Searched Batch">
      <node label ="a batch number" />
    </node>
    <node label="Compound Number">
      <node label ="a compound number" />
    </node>
    <node label="Parent Number">
      <node label ="a parent number" />
    </node>
    <node label="Chemist Name">
      <node label ="Name 1" />
    </node>
    <node label="Quantity Available">
      <node label ="N/A" />
    </node>
    <node label="Molecular Formula">
      <node label ="a molecular formula" />
    </node>
    <node label="Notebook Number">
      <node label="a notebook number" />
    </node>
    <node label="Analytical Images">
      <node label ="show some pdf files" />
    </node>
    <node label="Who has Registered Batches">
      <node label ="Name 1" />
      <node label ="Name 2" />
      <node label ="Name 3" />
      <node label ="Name 4" />
      <node label ="Name 5" />
    </node>
    <node label="Chemical Structure" >
      <node label="Click to view compound image" />
    </node>
  </node>
</graph>

Okay, I'd start with a base Graph class. 好的,我将从Graph基类开始。 From that I'd create a child class for each graph type each inheriting from the base Graph. 由此,我将为每个从基础Graph继承的图形类型创建一个子类。 The base Graph class is almost certainly going to have a method Draw() which is going to display the graph, this would be overridden by each child class allowing the graphs to each be drawn independently. 几乎可以肯定,基础Graph类将具有一个Draw()方法来显示图形,这将被每个子类所覆盖,从而允许分别绘制图形。 In drawing each graph you will be using appropriate objects to represent the graph and the functionality it needs, it's here that you'd vary the node types. 在绘制每个图时,您将使用适当的对象来表示图及其所需的功能,在这里您将改变节点类型。

Have a look at ZedGraph , that's a pretty comprehensive graphing/charting library which is open source so you can explore the object graph there and see how each varied chart type evolves. 看看ZedGraph ,这是一个非常全面的图形/图表库,该库是开源的,因此您可以在那里浏览对象图,并查看每种变化的图表类型如何演变。

从图的角度来看,我将对要在图中显示的对象使用一个接口,该接口应以一种通用的方式公开图的属性,例如键值对集合...

I would recommend looking into Composite Visitor Pattern . 我建议您研究“ 复合访客模式”

Sure you will not only need to draw this stuff, but also persist the graph, carry out commands, etc. 当然,您不仅需要绘制这些东西,而且还需要保留图形,执行命令等。

If you put the logic into the class hierarchy itself, it will eventually grow big and cluttered with conditional logic, with dependencies on UI components, etc. 如果将逻辑放入类层次结构本身中,它将最终变得庞大,并因条件逻辑,对UI组件的依赖性等而变得混乱。

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

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