UML 聚合可以是双向的吗?

Can UML aggregation be both ways?

我正在 Python 中使用以下 class 定义的 MVC 模式实现应用程序:

class Controller(object):
    def __init__(self, model, view):
        self.model = model
        self.view = view

        self.view.register(self)

class Model(object):
    def __init__(self):
        pass

class View(object):
    def __init__(self):
        self.controller = None

    def register(self, controller):
        self.controller = controller

类 由

实例化
model = Model()
view = View()
Controller(model, view)

Controller 可以访问 View,但 View 也可以访问 Controller(因为 Controller 将自身传递给了 View)。在 UML 中表示这种结构的合适方法是什么?我的猜测是

但我不确定这两种方式是否存在聚合。

(UML 2.5 第 9.5.3 节)

[composite] Indicates that the Property is aggregated compositely, i.e., the composite object has responsibility for the existence and storage of the composed objects

所以双向组合是没有意义的,因为你不能对存在承担双向责任——一旦一方摧毁了另一方,另一方就无法摧毁第一方。

此外,在您的 MVC 示例中使用聚合也是不正确的,因为控制器不负责模型的生命周期;事实上,一个模型可以用于许多不同的控制器。

所以只使用常规关联,具有单向和双向导航能力。

您正在使用共享构图(空心菱形)。引用 p。 110 个规格

Indicates that the Property has shared aggregation semantics. Precise semantics of shared aggregation varies by application area and modeler.

所以不建议一般使用它,除非你对它的语义有自己的定义。

如果你打算使用复合聚合,这只是禁止在两侧。这意味着每个连接元素的生命周期取决于另一个元素的生命周期。

引用 p. 110以下:

Compositions may be linked in a directed acyclic graph with transitive deletion characteristics; that is, deleting an object in one part of the graph will also result in the deletion of all objects of the subgraph below that object.

双向不是非循环的。