Relationships in a UML class diagram

。_饼干妹妹 提交于 2019-12-06 08:33:53

问题


I have an application that models a tree, with classes Tree, Node and Edge (I need the Edge class!), and I'm not sure how to represent the relationships in the class diagram for this. I've read other posts about this, but am still in doubt.

The Tree object have a pointer to a Node (its root), which I believe defines an one-way association (Tree -> Node) with multiplicity 1..1 in both ends. Is it right?

Each Node object have pointers to the edges that comes out of it (Edge objects). Since these edges only exist if the node exist, I believe it's a composition association.

But then I have, in each Edge object, a pointer to the target Node of the edge. How can I represent this relationship, considering I already have the Node -> Edge composition described above?

Also, if you're still reading :), each Node have a pointer to its parent Node. Here, I would use an one-way unary association, but I don't know which name to use for this relationship.

Thanks for any help.


回答1:


I'd say that:

  • A tree has a root node
  • Nodes have child nodes
  • Nodes may have a parent node

Note:

  • You might want to distinguish between a UML class diagram versus a UML object diagram
  • I don't know whether root node and child node are the same class (a child node might be a subclass of root node, because both types of node have children but only child nodes have a parent node)

Edge might not be a class at all: instead an Edge might just be an instance of a Node.




回答2:


From what you describe it sounds like the UML class diagram should have:

Tree -> Node, aggregation, 1..0-1
Node -> Edge, aggregation, 1..*
Edge -> Node, composition, 1..2 (an edge exists only if it connects 2 nodes)
Node -> Node, aggregation, 1..1 (would be composition, except the root node doesn't point to a node.

The difference between aggregation and composition can be thought of in terms of lifetimes. If two objects lifetimes are the same because they are co-dependent on each other then the relationship is one of composition, otherwise it is aggregation.




回答3:


--The Tree object have a pointer to a Node (its root), which I believe defines an one-way --association (Tree -> Node) with multiplicity 1..1 in both ends. Is it right?

No, the multiplicity should be 0..1 - 1 (not all nodes will be the root of a tree)

You should also think whether Edge is really a class or not. If you don't need to store any kind of information about the edges (i.e. they are not labelled), I'd just model edges as a binary associations between nodes




回答4:


The handling of a Tree structure in OO is solved by the Composite Design Pattern.

You seem to be slightly confused by the difference between aggregation and composition. Use composite when the lifetime of the whole and the part are equal, use aggregation where the lifetime of the parts is different, i.e. you add and/or remove parts from the collection.



来源:https://stackoverflow.com/questions/1501495/relationships-in-a-uml-class-diagram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!