简体   繁体   中英

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

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. 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. From that I'd create a child class for each graph type each inheriting from the base 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. 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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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