类图:我可以分享一个作品吗? [英] Class diagram: Can I share a composition?

查看:31
本文介绍了类图:我可以分享一个作品吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在使用 mvc 模式在 C++ 中制作一个简单的游戏,并拥有一个其生命周期由控制器类管理的世界对象.这感觉很明显像一个组合,但是如果说一个游戏逻辑类也需要对同一个世界的永久引用,或者这个世界的环境(世界所拥有的)需要引用它,这些只是常规关联吗?还是应该进行所有共享聚合?

Let's say I'm using the mvc pattern to make a simple game in c++ and have a world object whose lifetime is managed by a controller class. This feels clearly like a composition, but what if, say a game logic class needs a permanent reference to the same world as well, or environments to this world (who is owned by the world) needs references to it, are these just regular associations or should I make all shared aggregations?

让我感到困惑的是组合被定义为非共享".但这是否意味着不与任何其他关系共享,或者仅不与其他聚合和组合共享.此外,共享聚合通常如何在 C++ 中实现?我的意思是,必须有人管理一生.现在我们可以使用 shared_ptr,但是如果没有它们,所有共享的东西都是在 main 中创建并从那里传递(和销毁)吗?这让我想知道;在类图中对控制器建模是错误的,应该只建模模型部分吗?

What makes me confused is that composition is defined as 'unshared'. But does this mean not shared with any other relationship, or only not shared with other aggregations and compositions. Also, how is shared aggregation typically implementet in c++? I mean, someone must manage the lifetime. Now we can use shared_ptr, but without them are everything shared just created in main and passed (and destroyd) from there? Which leads me to wonder; is it wrong to model the controller in a class diagram, should only the model part be modelled?

作为我的主要问题的另一个示例(是否允许与 uml 类图中的任何其他类型的关系共享组合),例如汽车上的轮子可以与其他物体共享吗?如果发动机需要永久参考,应该如何建模?一个简单的关联?

As another example of my main question (are composites allowed to be shared with any other kind of relation in a uml class diagram) e.g. can a wheel on a car be shared with other objects? If the engine needs a permanent reference, how should it be modelled? A simple association?

推荐答案

是的,这是可能的,最好的例子之一就是游泳池(结构,而不是有水的结构......).在一个池中,池管理器与所有元素都具有组合关系.池管理器根据需要管理元素到用户"的分配.用户现在正在共享这些聚合元素.

Yes, it is possible and one of the best example is that of a pool (the struture, not the one with water...). In a pool, the pool manager has a composition relationship with all the elements. The pool manager manages the allocation of elements to "users" as required. The users are now sharing these agregate elements.

请务必注意,池管理器是池元素的所有者,因此负责它们的创建和销毁(根据需要将池维护在规定的限制内)并需要跟踪它们的分配.池元素也只能有一个所有者.在这种情况下,池的用户只能借用和归还一个元素.

It is important to note that the pool manager is the owner of the pool elements and is, therefore, responsible for their creation and destruction (as required to maintain the pool within prescribed limits) and needs to keep track of their allocation. The pool elements can also only have one owner. The pool's user can only borrow and return an element in this scenario.

在您的场景中,我怀疑您可以使用聚合或仅使用简单的关联,请记住,对象的所有者(具有组合的对象)可以销毁它...

In your scenario, I would suspect you can use either aggregations or just a simple association, keeping in mind that the owner of the object (the one with the composition) can destroy it...

这篇关于类图:我可以分享一个作品吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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