我做了一个例子。代表其他对象的对象集合的面向对象设计
我有一类这个类的
class Person
{
//stuff
}
对象用在这里和那里等,但我会喜欢做单独的类,这将是负责绘制者。所以我需要例如。屏幕上的Person
对象的位置。
我可以从Person
继承:
class PersonVis : Person
{
Point location;
}
但我不知道PersonVis
应该有“是”与Person
关系。
我可以做一个字典,Person
关键和PersonProperties
为值:
class PersonProperties
{
Point location;
}
但是我有一个情况,其中PersonProperties
类的属性一个取决于Person
状态,所以也许不是使用字典,我应该使用只是一个列表:
class PersonProperties
{
Person person;
Point location;
}
但如果我这样做,我必须非常小心插入的PersonProperties
新对象到该列表时,因为我最终可能会得到两个内部具有相同Person
实例的对象。
在您看来,最佳选择是什么?
编辑
我想我必须拿出真实的例子。
我有类Graph
class Graph
{
List<Vertex> Vertices;
//other stuff
}
class Vertex
{
//stuff not related to drawing at all
}
我使用这个类来解决问题。我根本不需要图形表示,我不需要例如顶点的位置。
现在我创建的类绘制此图:
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
}
}
顶点例如可以是。移动,因此绘图更改和图形的属性必须重绘。
XXX是保存关于例如信息的结构。当前点的顶点。
我不知道我应该选择什么样的XXX。
XXX可能是:Dictionary<Vertex, VertexProperties>
其中VertexProperties
:
class VertexProperties
{
Point location;
}
但是我有一个情况,其中VertexProperties
类的属性一个取决于Vertex
状态。因此,也许List<VertexProperties>
其中VertexProperties
:
class VertexProperties
{
Vertex vertex;
Point location;
}
但如果我这样做,插入的VertexProperties
新对象到该列表时,我必须非常小心,因为我可以用里面的相同Vertex
实例的两个对象结束。
也许List<VertexVis>
其中VertexVis
:
class VertexVis : Vertex
{
Point location;
}
但是,这是相同以前+我不觉得VertexVis
“是” Vertex
。
是的,我今天想知道它,现在正在实施它。我决定有例如。 'Vertex'对象和子类:'DrawableVertex'带有附加字段。该视图将得到'DrawableGraph'。我正在使用C#,所以我决定有'Graph其中T:Vertex'和'DrawableGraph:Graph '(我忽略了边)。我不知道这是不是个好主意,但现在代码看起来更清晰。 –
prostynick
2010-06-23 17:17:12