UML 关联与组合和细节级别
实际上,这是几个业余 UML 问题!当创建 UML 图来对某些领域概念进行建模时,您遇到一个领域概念“保存”有关另一个概念的一些信息,那么保存对该实体的标记/引用还是将整个实体保存在模型本身中更好?请记住,这与创建一个简单的高级模型有关 - 我确信在实施阶段事情会略有不同。
例如,以下两个模型中哪一个实际上是正确的?第一个是组合关系,FlightBooking 持有整个 Flight。在第二个中,FlightBooking 仅引用了 Flight。
其次,在创建高级 UML 图时建模领域概念时,您真正想要了解多少细节?例如,在下图中,航班可以将有关出发地/目的地的详细信息保存为字符串,或者我可以为这些概念建模单独的类并创建组合关系。两者中哪一个是可取的?
另外,还有一件事,在建模时上面的航班将出发地/目的地“保存”为另一个类而不是字符串,这两种方法中哪一种是对此进行建模的正确方法?我很困惑什么时候展示关联,什么时候展示组合。
Actually, make that a couple of amateur UML questions! When creating a UML diagram to model some domain concepts and you come across a domain concept that "holds" some information about another concept, is it better to hold a stamp/reference to that entity or hold the whole entity in the model itself? Please bear in mind that this is relating to creating a simple high-level model - I'm sure in the implementation stage things would be slightly different.
For example, which of the two models below is actually correct? The first one has a composition relationship, with FlightBooking holding the whole of Flight. In the second one, FlightBooking just has a reference to Flight.
Secondly, when creating a high level UML diagram modelling domain concepts, how much detail are you really meant to go? For example, in the diagram below, a flight could hold details about origin/destination as strings, or I could model separate classes for these concepts and create a composition relationship. Which of the two is advisable?
Also, just another thing, when modelling the above where a Flight "holds" an orig/destination as another class rather than a string, which of the two ways is the correct way of modelling this? I'm quite confused as to when to show assosciation and when to show composition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
协会:
这意味着两个类有某种关系,可以是任何关系。
例如:A 使用 B,A 以给定方式与 B 相关。
成分:
这也是用于建模“所有权”的特殊类型的关联。这与聚合非常相似,唯一的区别是它描述了整体-部分关系,并且“部分”实体没有自己独立的存在,
例如:A 由 B 组成; B 是 A 的一部分,因此没有 A 就无法存在。
很好的解释:UML类图:关联、聚合和组合
Association :
It means two classes have some kind of relationship, could be anything really.
E.g : A uses B, A is related to B in a given way.
Composition :
This is also a special type of association used for modelling 'Ownership'. This is very similar to Aggregation with the only difference that it depicts a Whole-part relationship and the 'part' entity doesn't have its own independent existence
E.g : A consists of B; B is a part of A and hence cannot exist without A.
Good explanation : UML Class Diagram: Association, Aggregation and Composition
抱歉,如果这有点长...
如果您正在尝试对领域概念进行建模,那么我鼓励您忘记组合/聚合并坚持使用简单的关联。为什么?因为决定组合/聚合会妨碍重要问题的解决。它们是:
关系命名 您可以通过命名关系端来完成 (1)。不使用角色名称(例如第一个示例中的“航班”),因为这没有告诉您为什么这种关系存在。那么在你的例子1中:这种关系代表什么?是航班上预留的座位吗?已确认的吗?付费了吗?从目前的图表中无法看出。基于动词的命名有多种方法,请参见 这篇文章。为什么要这样做?因为它会提示您确保您了解该域。很大一部分(可能是大多数,尽管我从未证明过)域规则存在于关系中。因此,理解这些关系存在的原因是理解该领域的基础。
基数可能比命名更明显。您应该确定两端 - 上部和下部。下端重要的是它是否可选(即 0 或 1)。在上端,1 个或多个。这再次揭示了该领域的重要规则。再次回到你的例子:一次航班预订中有多少个航班?一个航班可以多次预订吗? (无论“in”是什么意思......)。
创建/删除行为 谁创建关系实例?谁删除?如果一端被删除,另一端会发生什么?再次强调,所有重要规则均来自问题领域。
希望这些也能回答你的第二个问题。不要吝惜人际关系。花时间去理解它们。它们是理解领域的秘诀。回答上述所有问题将使您比尝试在组合/聚合/关联之间做出决定更深入地了解。
如果您确实想要展示组合与关联,但是如果您回答上述问题,那么它就是机械的:
否则使用简单关联。至于聚合:忽略它。将其从你的词汇表中删除。造成的混乱超过其价值。通过聚合,您可以说的所有内容都可以通过正确定义的简单关联来更清晰、更准确地表达。
嗯。
PS:语法要点:构图使用实心菱形。你所展示的是聚合。
Sorry if this is a bit long...
If you're trying to model domain concepts then I'd encourage you to forget about composition/aggregation and stick with simple associations. Why? Because deciding on composition / aggregation gets in the way of the important questions. They are:
Relationship naming You accomplish (1) by naming the rel ends. Not with role names (e.g. "Flights" in your first example) because that tells you nothing about why the relationship exists. So in your example 1: what does the relationship represent? Is it a reserved seat on the flight? A confirmed one? Paid for? Impossible to tell from the diagram as it stands. There are various approaches for verb-based naming, see e.g. this post. Why do this? Because it prompts you to make sure you understand the domain. A large percentage (probably a majority, although I've never proven it) of the domain rules exist in the relationships. So understanding why the relationships exist is fundamental to understanding the domain.
Cardinality Probably more obvious than naming. You should determine at both ends - both upper and lower. Important at the lower end is whether it's optional (i.e. 0 or 1). At the upper end, 1 or many. Again this surfaces important rules from the domain. Coming back to your example again: how many flights in a flight booking? Can one flight be in multiple bookings? (whatever 'in' means...).
Create / Delete Behaviour Who creates instances of the relationship? Who deletes? If one end gets deleted, what happens to the other end? Again, all important rules from the problem domain.
Those hopefully answer your second question too. Don't skimp on relationships. Take time to understand them. They are the secret sauce to understanding a domain. Answering all the questions above will give you far more insight than trying to decide among composition/aggregation/association.
If you really want to show composition vs association however it is mechanical if you answer the questions above:
Otherwise use a simple association. As for aggregation: ignore it. Remove it from your vocabulary. Causes more confusion than it's worth. Everything you can say with aggregation you can say far more clearly and precisely with a properly defined simple association.
hth.
PS: a point of syntax: Composition uses a filled diamond. What you've shown is Aggregation.
组合、聚合和关联。
基本上,
组合
、共享
甚至无
,这三者都是聚合。因此, 组合 是 聚合 的子集,而 聚合 是 关联的一个功能
Compositions, aggregations and associations.
composition
,shared
and evennone
, all three are aggregations.So, composition is a subset of aggregation and aggregation is a feature of the association
对于您的第一个示例,第一个带有组合的图表是正确的。您的第二个选项,将 FlightID 引用为 int,可以工作,但它是不完整的:该 int 本身并不能为您提供访问 Flight 对象的方法。 Flight 类不会神奇地存储您可以按编号检索的航班对象列表,因此第二个选项将需要第三个类来存储所有 Flight 对象。
对于你的第二个问题,在高层次上,这实际上取决于个人偏好(或你教授的个人偏好!)。只是尝试使用你的最佳判断。
For your first example, the first diagram, with composition, is correct. Your second option, referring to flightID as an int, could work but it's incomplete: that int by itself doesn't give you a way to access the Flight object. The Flight class doesn't magically store a list of flight objects that you can retrieve by number, so that second option would need a third class somewhere to store all the Flight objects.
For your second question, at the high level it really depends on personal preference (or your professor's personal preference!). Just try to use your best judgement.