简体   繁体   English

在另一个类中创建一个类的对象,UML类图中的关系是什么?

[英]Creating an object of one class in another, what is the relationship in a UML class diagram?

I have a SessionManager class, that I create objects of in every class a user needs to be logged in to reach I am unsure of the type of relationship created here as I seem to have confused myself with all of the definitions online. 我有一个SessionManager类,在每个用户都必须登录才能访问的类中创建对象,我不确定在此创建的关系的类型,因为我似乎将自己与所有在线定义混淆了。 I am trying to create a UML class diagram representing all of the classes in my application. 我正在尝试创建一个表示我的应用程序中所有类的UML类图。 Would this be an Aggregation relationship or simply an associated relationship? 这是聚集关系还是仅仅是关联关系?

I have no classes that extend each other as I have made an Android app that uses intents to pass from activity to activity, I'm simply creating objects of some classes in other classes. 我没有一个相互扩展的类,因为我制作了一个使用意图在活动之间传递的Android应用程序,我只是在其他类中创建某些类的对象。

Additionally, is there any way to represent an intent on a UML class diagram? 此外,有什么方法可以表示UML类图上的意图吗?

Creating SessionManager Object: 创建SessionManager对象:


SessionManager sessionManager;

Using checkLogin() method from SessionManagerClass in onCreate method: 在onCreate方法中使用SessionManagerClass中的checkLogin()方法:


sessionManager = new SessionManager(this);
        sessionManager.checkLogin();

I think Composition relationship is correct in this case. 我认为在这种情况下构图关系是正确的。

class A {
   private B b = new B();
}
//or
class A {
   private B b;
   public A() {
      b = new B();
   }
}

Composite aggregation is about runtime. 复合聚合与运行时有关。 Composed elements are to destroyed along with the composing element. 组成元素将与组成元素一起销毁。 The table on p. 第p页的表。 110 of UML 2.5 states UML 2.5状态中的110个

composite | 复合材料 | Indicates that the Property is aggregated compositely, ie, the composite object has responsibility for the existence and storage of the composed objects (see the definition of parts in 11.2.3). 指示该属性是复合聚合的,即,复合对象负责所组成对象的存在和存储(请参见11.2.3中各部分的定义)。

It seems appropriate to model this (filled diamond at the association end) in your case. 在您的情况下,对此建模(在关联端为实心菱形)似乎是合适的。 The SessionManager should be some kind of factory producing Session objects which it holds responsibility for. SessionManager应该是工厂负责生产的Session对象。

It would have HAS-A relationship, in other words, Aggregation. 它将具有HAS-A关系,即聚合。

Aggregation is a special form of association. 聚集是一种特殊的关联形式。 It is a relationship between two classes like association, however its a directional association, which means it is strictly a one-way association. 它是两个类别(如关联)之间的关系,但是它是方向关联,这意味着它严格来说是单向关联。 It represents a HAS-A relationship 它代表了HAS-A关系

UML Construction Demo UML建设演示

聚合UML示例

聚合UML

Source 资源

More info: 更多信息:

Aggregation vs composition 聚合与构成

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

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