比方说,我有三个班,Solid
,Face
,并Edge
被定义如下:是否存在处理这种共享资源情况的设计模式或基本的面向对象原则?
class Solid{
public:
// perform an action on a single edge.
void addFillet(int edgeNum);
// perform an action on a single face
void addBore(int faceNum);
// perform an action on all faces and edges
void move(Pos newPosition);
private:
std::vector<Edge*> edges;
std::vector<Face*> faces;
};
class Face{
public:
// will modify a subset of edges
virtual void changeHeight(int newHeight) = 0;
private:
int myNum;
std::vector<Edge> edges;
}
class Edge{
public:
virtual void changeLength(int newLength) = 0;
private:
int myNum;
int length;
}
在这个例子中
,Solid
管理着的Edge
S '超'。 Solid
管理的每个Face
将具有Solid.edges
的“子集”。此外,任何两个Solid.faces
可能有一个共同的Edge
。
我的问题:有没有任何设计模式或面向对象的一般原则来处理这种情况?我如何管理Solid.edges
和Face.edges
之间的关系?更具体地说
那么通常你只能将边缘缓冲区存储在Solid对象中,并且对于每个Face存储它拥有的Edge的索引。 (与存储顶点的传统计算机渲染并行绘图) – meowgoesthedog
由于@meowgoesthedog提到,这与OpenGL处理[Vertex Arrays](https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Array_Object)的方式类似。一个对象拥有(我的意思是处理分配)实际顶点,而其他对象通过id引用它们。 – CoryKramer
您可能想要在'Solid'和'Face'中保存'shared_ptr'。除非'Solid'是最终的拥有者,否则你可能需要'Face'中的'weak_ptr'。 –
AndyG