这是聚合吗? [英] Is this an aggregation?

查看:141
本文介绍了这是聚合吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

 public static void main(String[] args) {
    Engine engine = new Engine("This Engine!");
    Car b = new Car(engine);

    b = null;
}

这是聚合吗?我总是认为聚合是指即使另一个类被摧毁,某些东西仍然可以生存......例如Car and Engine。如果我删除Car实例,引擎仍然可以存在。

Is this an aggregation? I always think that an aggregation is when something can still "live" even if the other class is "destroyed"... so for example Car and Engine. if I delete the Car instance, the Engine can still live.

这是正确的吗?

推荐答案

首先,我会说你的代码看起来会更好

First, I would say that your code would look better when you do

Car b = new Car();
b.setEngine(engine);

看起来会更好。

然后关于你的聚合思维,是的,你是对的。 不知何故。但是,如果你真的分裂你的对象细粒度。我的意思是,如果没有发动机,你会有车吗? :)

Then regarding your aggregation thinking, yes, you are correct. "Somehow". But this if you really split your "objects" fine grained. I mean, will you have a Car if it does not got an engine? :)

我实际上会看到这辆车,我想更多的是汽车和司机的聚合。如果汽车损坏,你仍然有一个司机。如果一个司机死了,你仍然有一辆车。

I would actually see the car as it is, I would think more an aggregation for a Car and a Driver. If Car is damaged still you have a Driver. And if a Driver dies, still you have a Car.

来自 uml 关于聚合:

聚合是有关联关系的变体;聚合比关联更具体。它是一个代表整体或部分关系的协会。作为一种关联,可以命名聚合并使用与关联相同的装饰。但是,聚合可能不涉及两个以上的类;它必须是一个二元关联。

Aggregation is a variant of the "has a" association relationship; aggregation is more specific than association. It is an association that represents a part-whole or part-of relationship. As a type of association, an aggregation can be named and have the same adornments that an association can. However, an aggregation may not involve more than two classes; it must be a binary association.

当一个类是其他类的集合或容器时,可能会发生聚合,但所包含的类没有强大的生命周期依赖关系。容器。容器的内容不会自动销毁。

Aggregation can occur when a class is a collection or container of other classes, but the contained classes do not have a strong lifecycle dependency on the container. The contents of the container are not automatically destroyed when the container is.


组合/聚合

composition/aggregation

与汇总混淆的另一种关系是构成。当聚合描述有关系时,该组合执行包含/部分关系。正如我所说的,通常情况下,引擎是你汽车对象权利(组成)的一部分。 Car对象有一个Driver(聚合)。

Another relation which confuses with aggregation is the composition. When aggregation describes the "has" relation the composition do the "contains/part of" relation. As I said, normally, the engine is part of for you car object right (composition). The Car object has a Driver (aggregation).

这篇关于这是聚合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆