UML 关联与组合和细节级别

发布于 2024-11-08 18:41:41 字数 655 浏览 10 评论 0原文

实际上,这是几个业余 UML 问题!当创建 UML 图来对某些领域概念进行建模时,您遇到一个领域概念“保存”有关另一个概念的一些信息,那么保存对该实体的标记/引用还是将整个实体保存在模型本身中更好?请记住,这与创建一个简单的高级模型有关 - 我确信在实施阶段事情会略有不同。

例如,以下两个模型中哪一个实际上是正确的?第一个是组合关系,FlightBooking 持有整个 Flight。在第二个中,FlightBooking 仅引用了 Flight。

http://imageshack.us/m/96/2227/flightiu.png

其次,在创建高级 UML 图时建模领域概念时,您真正想要了解多少细节?例如,在下图中,航班可以将有关出发地/目的地的详细信息保存为字符串,或者我可以为这些概念建模单独的类并创建组合关系。两者中哪一个是可取的?

http://imageshack.us/m/23/3395/flight2s.png

另外,还有一件事,在建模时上面的航班将出发地/目的地“保存”为另一个类而不是字符串,这两种方法中哪一种是对此进行建模的正确方法?我很困惑什么时候展示关联,什么时候展示组合。

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.

http://imageshack.us/m/96/2227/flightiu.png

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?

http://imageshack.us/m/23/3395/flight2s.png

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

友谊不毕业 2024-11-15 18:41:41

协会
这意味着两个类有某种关系,可以是任何关系。
例如: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

尘曦 2024-11-15 18:41:41

抱歉,如果这有点长...

如果您正在尝试对领域概念进行建模,那么我鼓励您忘记组合/聚合并坚持使用简单的关联。为什么?因为决定组合/聚合会妨碍重要问题的解决。它们是:

  1. 为什么这种关系存在?具体来说,它捕获了哪些领域规则/约束?
  2. 两端的基数是多少?
  3. 什么是创建和创建?删除行为?即谁创建/删除关联实例?

关系命名 您可以通过命名关系端来完成 (1)。不使用角色名称(例如第一个示例中的“航班”),因为这没有告诉您为什么这种关系存在。那么在你的例子1中:这种关系代表什么?是航班上预留的座位吗?已确认的吗?付费了吗?从目前的图表中无法看出。基于动词的命名有多种方法,请参见 这篇文章。为什么要这样做?因为它会提示您确保您了解该域。很大一部分(可能是大多数,尽管我从未证明过)域规则存在于关系中。因此,理解这些关系存在的原因是理解该领域的基础。

基数可能比命名更明显。您应该确定两端 - 上部和下部。下端重要的是它是否可选(即 0 或 1)。在上端,1 个或多个。这再次揭示了该领域的重要规则。再次回到你的例子:一次航班预订中有多少个航班?一个航班可以多次预订吗? (无论“in”是什么意思......)。

创建/删除行为 谁创建关系实例?谁删除?如果一端被删除,另一端会发生什么?再次强调,所有重要规则均来自问题领域。

希望这些也能回答你的第二个问题。不要吝惜人际关系。花时间去理解它们。它们是理解领域的秘诀。回答上述所有问题将使您比尝试在组合/聚合/关联之间做出决定更深入地了解。

如果您确实想要展示组合与关联,但是如果您回答上述问题,那么它就是机械的:

  1. 如果关系一端的基数是 1:1(即只能是 1),并且
  2. 同一端负责创建和删除另一端的实例,那么
  3. 它可以显示为组合。

否则使用简单关联。至于聚合:忽略它。将其从你的词汇表中删除。造成的混乱超过其价值。通过聚合,您可以说的所有内容都可以通过正确定义的简单关联来更清晰、更准确地表达。

嗯。

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:

  1. Why does the relationship exist? Specifically, what domain rules/constraints does it capture?
  2. What's the cardinality at either end?
  3. What's the create & delete behaviour? i.e. who creates / deletes instances of the association?

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:

  1. IF the cardinality on one end of the relationship is 1:1 (i.e. can only ever be one), AND
  2. that same end is responsible for creating and deleting instances of the other end, THEN
  3. It can be shown as composition.

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.

╭ゆ眷念 2024-11-15 18:41:41

组合、聚合和关联。

  • 在做其他事情之前,你应该先了解 A 到 B 的关联是什么。
    • 基本上,它是 A 和 B 之间的一条实线。它可以表示一个将 A 的类/实例与 B 的类/实例连接起来的结构。该结构可以是任何类型并且属于任何地方。有关该线路的所有信息都描述了此结构。
    • 如果有两个结构,一个结构将 A 的一个实例与 B 的实例连接,另一个结构将 B 的实例与 A 的实例连接,则可以在一个关联中显示它们。然后,关于其B端写入的信息描述第一结构(b→a),关于另一端的信息描述另一个结构。
    • 如果从 A 到 B 有多个结构,则必须绘制两个不同的关联。
    • 如果连接结构很复杂,您可以将其表示为关联类。您可以在那里定义更多详细信息。
    • 连接结构可以连接两个以上的类,然后它将显示为一个大菱形,并具有指向这些类的实心分支。依然是协会!注意:现有工具对这两个更复杂的关联的支持非常差。你可以很容易地用它们创造出一些完全毫无意义的东西。而且它们很困难。小心使用。
    • 基本上,

  • 在C++中,实例A可以不通过指针,而是直接获得B实例。它没有特殊的 UML 符号,它应该以与普通指针属性相同的方式显示。

示例类图


  • 构图由所谓的黑色或全菱形显示。它位于容器的侧面。
  • 另一种是空菱形,称为“共享聚合”,简称“共享”。它没有严格定义,您可以使用它来创建自己的标准。当然,将其放在项目容器关联的项目一侧是愚蠢的。但它很容易处于关联的两端。
    • 为什么合成钻石只能在一侧?因为组合意味着,项目仅在容器(或容器本身)存在对它们的引用时才存在。当然,这不可能对双方都有利。
    • 相反,双方“共享”通常是有意义的。例如,Student 实例可以包含所有参加的课程的列表,而 Courses 实例可以包含所有参加的学生的列表。
  • 通常,您可以看到名称“聚合”用于“共享聚合”。这是一个严重的错误。因为,根据标准,组合共享甚至,这三者都是聚合。

因此, 组合 聚合 的子集,而 聚合 关联的一个功能

Compositions, aggregations and associations.

  • Before other things you should understand what the association A to B is.
    • Basically it is a solid line between A and B. It can represent one structure that connects class/instanc(es) of A with the class/instances of B. The structure can be of any sort and belong anywhere. All information, written about the line, describes this structure.
    • If there are two structures, one structure, that connects one instance of A with instance(s) of B and another structure that connects instance of B with instance(s) of A, you can show them both in ONE association. Then, information written about its B end describes the first structure (b->a) and info about the other end describes the other structure.
    • If you'll have more than one structure guiding from A to B, you have to draw two different associations.
    • If a joining structure is complex, you could represent it as an Association Class. There you can define more details.
    • A joining structure can connect more than two classes, then it will be shown as a large diamond with solid branches to these classes. It is still association! Attention: these two more complex associations are very badly supported by existing tools. You can easily create something absolutely senseless with them. And they are difficult. Use carefully.
  • In C++ instance A can have the B instance not by pointer, but directly. There is NO special UML sign for it, it should be shown in the same way as normal, pointer attribute.

example Class diagram


  • Composition is shown by what is called black or full diamond. It is on the side of the container.
  • The other one, empty diamond, is called "shared aggregation", or, shortly, "shared". It is not strictly defined and you can use it creating your own standard. Of course, it would be foolish to put it on the item side of item-container association. But it easily could be on the both ends of association.
    • Why the composition diamond can be only on one side? Because composition means, that items exist ONLY while exist reference to them from the container (or container itself). Of course, that couldn't work for both sides.
    • On the contrary 'shared' on both sides often has sense. For example, Student instances can have a list of all courses attended, and Courses instance can have a list of all students attending.
  • Often you can see name "aggregation" used for "shared aggregation". It is a BAD mistake. Because, according to standard, composition, shared and even none, all three are aggregations.

So, composition is a subset of aggregation and aggregation is a feature of the association

最好是你 2024-11-15 18:41:41

对于您的第一个示例,第一个带有组合的图表是正确的。您的第二个选项,将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文