简体   繁体   English

当A类与B类具有聚合和组合关系时,如何绘制UML图?

[英]How to draw a UML diagram when class A has aggregation and composition relationship with class B?

I have two questions to ask. 我有两个问题要问。 So let's assume there's a class A and B which are defined like these. 所以我们假设有一个类A和B这样定义。

1. 1。

class A {
    private B b;
    private B otherB;
    public A(B otherB)
    {
        this.otherB = otherB; 
    }
}
class B {
}

So class A has a composition relationship with the variable b and aggregation relationship with the variable otherB. 因此,A类与变量b具有组合关系,与变量otherB具有聚合关系。 How can I draw this in UML diagram. 我如何在UML图中绘制它。

2.Would the following case be still a composition relationship? 2.以下案件是否仍然是一种构成关系?

class A
{
    private B b;
    public B getMethod(){
        B newB = new B();
        newB.bValue = b.bValue;
        return newB;
    }  
}
class B
{
    private int bValue;
}
  1. As other comments/replies have pointed out, there is not problem of having different associations (composed or not) between the same classes. 正如其他评论/回复所指出的那样,不存在在同一类之间具有不同关联(组成与否)的问题。 组合与聚合

  2. From the implementation point of view (and this also applies to the previous question) you need to understand what a composition association means . 从实现的角度来看(这也适用于上一个问题),您需要了解组合关联的含义

Basically, if we have instance specification a1 and a2 (as instances of the class A ), only one of them could compose an instance b1 (as instance of the class B ) via the role (association end) "composesB" of the composite association. 基本上,如果我们有实例规范a1a2 (作为类A的实例),那么只有其中一个可以通过复合关联的角色(关联结束)“composesB”组成实例b1 (作为类B的实例) 。

Likewise, provided that a1 composes b1 via the "composesB" role of a composite association, everytime a1 gets "destroyed", b1 should also be "destroyed". 同样,假设a1通过复合关联的“composesB”角色组成b1 ,每次a1被“销毁”时, b1也应该被“销毁”。 Instead, this wouldn´t occur if a1 object aggregated b1 via the "aggregatesB" role of an aggregate association. 相反,如果a1对象通过聚合关联的“aggregatesB”角色聚合b1 ,则不会发生这种情况。

As you may imagine, from an implementation point of view, you need much more than a field and a simple method in a class in order to support a composite association between two classes. 您可以想象,从实现的角度来看,您需要的不仅仅是字段和类中的简单方法,以支持两个类之间的复合关联。

Update: to include an example. 更新:包含一个示例。

For example, EMF is an implementation of the EMOF specification (it´s not UML) in which the concept of a containment reference (similar to the concept of a composite association) can be depicted as follows. 例如,EMF是EMOF规范(它不是UML)的实现,其中包含引用的概念(类似于复合关联的概念)可以描述如下。 In our particular case: 在我们的特定情况下:

生成复合引用的代码

Going away from technical details, you may grasp that when you set a B instance as part (contained, composed) by an A instance object. 远离技术细节,当您将A实例设置为A实例对象的一部分(包含,组合)时,您可能会意识到这一点。 You firstly have to check that the former might be contained in a different A instance via the same containment reference, if so, such B instance needs to be removed from the old A instance one. 您首先必须通过相同的包含引用检查前者是否可能包含在不同的A实例中,如果是这样,则需要从旧的A实例中删除此类实例。

  1. Creating Multiple Associations Between Classes is, in fact, legal. 事实上, 在类之间创建多个关联是合法的。 The recommendation is to assign a role to each relationship when you do so. 建议在您执行此操作时为每个关系分配一个角色。

  2. I'm going to say yes, that is still a composition relationship (though others may disagree). 我要说是的,这仍然是一种构成关系(尽管其他人可能不同意)。 To make my case, we look at the simple IBM definition showing Student composed of Schedule (presumably amongst other things). 为了说明我的情况,我们看一下简单的IBM 定义,显示由Schedule组成的Student(可能是其他的东西)。 It's very easy to imagine that there would also be a Teacher composed of Schedule, and that a Teacher might want to get the Schedule of a Student, or vice versa. 很容易想象,还会有一个由附表组成的教师,教师可能想要获得学生的时间表,反之亦然。 Does the getting of a Schedule by another class invalidate the composition relationship? 另一个类获得一个附表是否会使作文关系无效? I think not; 我想不是; or at least, the IBM definition does not appear to be so narrow as to preclude that possibility. 或者至少,IBM的定义似乎并不那么狭隘以至于排除了这种可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM