I made up an example.
I have a class
class Person
{
//stuff
}
Objects of this class are used here and there etc. But I will like to make separate class, that will be responsible for drawing persons. So I will need eg. a location of Person
object on the screen.
I could inherit from Person
:
class PersonVis : Person
{
Point location;
}
But I am not sure that PersonVis
should have "is a" relation with Person
.
I could make a dictionary with Person
as key and PersonProperties
as value:
class PersonProperties
{
Point location;
}
But I could have a situation, where one of property of PersonProperties
class depends on state of Person
, so maybe instead of using dictionary, I should use just a list of:
class PersonProperties
{
Person person;
Point location;
}
But if I do that I must be very careful when inserting new object of PersonProperties
to that list, because I could end up with two objects with the same Person
instance inside.
In your opinion, what is the best choice?
EDIT
I think I have to come out with real example.
I have class Graph
class Graph
{
List<Vertex> Vertices;
//other stuff
}
class Vertex
{
//stuff not related to drawing at all
}
I am using this class to solve problems. I don't need graphic representation at all and I don't need for example location of the vertex.
Now I created class for drawing this graph:
class GraphDrawer
{
Graph GraphToDraw;
XXX xxx;
void OnDraw()
{
//use vertices from graph, but use also stored positions of the vertices
//and any other things related to drawing
}
}
Vertices can be eg. moved, so properties of drawing changes and graph must be redraw.
XXX is the structure that holds information about eg. current points of vertices.
I don't know what kind of XXX I should choose.
XXX could be: Dictionary<Vertex, VertexProperties>
where VertexProperties
:
class VertexProperties
{
Point location;
}
But I could have a situation, where one of property of VertexProperties
class depends on state of Vertex
. So maybe List<VertexProperties>
where VertexProperties
:
class VertexProperties
{
Vertex vertex;
Point location;
}
But if I do that I must be very careful when inserting new object of VertexProperties
to that list, because I could end up with two objects with the same Vertex
instance inside.
And maybe List<VertexVis>
where VertexVis
:
class VertexVis : Vertex
{
Point location;
}
But this is the same as in previous + I don't feel that VertexVis
"is a" Vertex
.