UML 聚合可以是双向的吗? [英] Can UML aggregation be both ways?

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

问题描述

我正在 Python 中实现一个具有 MVC 模式的应用程序,其类定义如下:

I am implementing an application with an MVC pattern in Python with the following class definitions:

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 中表示这种结构的合适方法是什么?我的猜测是

Controller has access to View, but View also has access to the Controller (since Controller passes itself to View). What is the appropriate way to represent such structure in UML? My guess would be

但我不确定两种方式的聚合是否存在.

but am not sure the aggregation in both ways exists.

推荐答案

(UML 2.5 section 9.5.3)

(UML 2.5 section 9.5.3)

[composite] 表示Property 是复合聚合的,即复合对象负责组合对象的存在和存储

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

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

So having a bi-directional composition makes no sense, as you cannot have bi-directional responsibility for existence -- once one side destroys the other, the other cannot destroy the first.

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

Furthermore using aggregation in you MVC example is also incorrect, as the controller is NOT responsible for the lifecycle of the model; in fact a model can be used from many different controllers.

因此只需使用具有单向和双向导航能力的常规关联即可.

So just use regular associations, with unidirectional and bidirectional navigability.

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

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