英文:
How to avoid circular association (Class Diagram)
问题
我有一个Project类,其中包含一个Objective。一个Objective可以有多个Projects。一个Meeting可以与一个Project(其中包含一个Objective)或Objective相关联。如何在类图中表示这种关系?
我知道不应该这样做。
英文:
I have a Project class that has one Objective. an Objective that has many Projects. a Meeting that can be associated to a Project (which has an Objective) or Objective. How can I represent this relationship in a Class Diagram
答案1
得分: 3
在UML中,存在循环关联绝对没有问题。这在你建模现实生活领域时非常常见。你甚至可以有自关联类在一个循环关联中,这仍然不会成为问题。
你唯一的建模挑战是准确表示:
> 一个Meeting
可以与一个Project
(它有一个Objective
)或Objective
相关联。
因此,你首先需要澄清:
- 它可以是两者之一吗(你的图表已经包含了这一点)?
- 它可以是两者都吗?如果可以:
- 会议的目标是否可以与项目目标不同(你的图表仍然包含了这一点)?
- 还是会议和项目的目标必须保持一致(请参阅我的其他答案,如何使用OCL表示直接和间接关联类之间的约束)?
- 它必须是其中一个,而不能同时是两者吗(请参阅这个问题以获取有关此主题的最佳最近答案)?
英文:
There is absolutely no problem in UML with circular associations. This is common business as soon as you model real life domains. You could even have self-associating classes in a circular association, and it still wouldn't be an issue.
Your only modelling challenge is to represent accurately:
> A Meeting
that can be associated to a Project
(which has an Objective
) or Objective
.
You must therefore first disambiguate:
-
can it be none of the two (your diagram gets it) ?
-
can it be both? If yes:
- can the meeting's objective be different from the project objective (your diagram still gets it) ?
- or shall objectives of the meeting and the project be aligned (see my other answer here, how to express constraints between direct and indirect associated classes using OCL)
-
must it be the one or the other but not both (see this question for the best recent answers on this topic)?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论