开发者

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 h开发者_如何学JAVAave 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.


From the graph perspective I would use an interface for the objects you want show in your graph, this interface should expose the properties for the graph in a common way e.g. a key value pair collection...


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜