This is an open MVC related question and I’m looking for critique and advice.
brief version:
ViewA is used to view ModelA.
If ModelB extends ModelA and ViewB is used to view ModelB would ViewB inherit from ViewA?
background:
I have created a Graph class, containing an array of vertices and an array of edges.
The vertices have each a n-sized array of integer data.
I also created a GraphViewer class for 2d-Graphs, that simply draws lines for edges and dots for vertices.
Now I decided to create a GraphEditor Class, that is a graph with editing capabilities, for instance it needs to have methods and fields that allow user interaction:
example:
selectVertex – a method that accepts the current mouse coodinates and returns the vertex closest to that point, and
selectedVertices – an array of all vertices selected
I decided that GraphEditor will inherit from Graph.
Because GraphEditor is still a graph only with editing capabilities.
Now I wanted to create a GraphEditorViewer that will need to draw selected vertices in a different color(for instance) and I thought since GraphEditor extends Graph, perhaps GraphEditorViewer should extend GraphViewer, however I find that is not the case since the new class GraphEditorViewer does not ‘view’ regular Graphs.
So I decided to use a ‘has-a’ relationship between GraphEditorViewer and GraphViewer.
That meant that I had to expose a lot of rendering related methods of GraphViewer so I could call them from the new class.
I am wondering if you would go about it differently and what do you think about my choice.
Thanks
This is of course open for discussion and a matter of subjective opinion:
Answer:
If ModelB extends (inherits) from ModelA and ViewA is used to view ModelA, then ViewB should not extend (inherit) from ViewA, reasons:
ViewB however should expect a ModelB, so inheriting ViewA is not recommended in this case.
You can however, use a composition (ViewB has a ViewA) relationship and use that to render ModelB as a ModelA and add additional details using more specific functions.