Class diagram: Can I share a composition?

╄→尐↘猪︶ㄣ 提交于 2019-12-22 11:28:40

问题


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?

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?

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?


回答1:


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...




回答2:


Taking your car-part of the question, if you mean if it is legal to use both white and black diamonds in the same picture like in this one..

..then I think it is perfectly valid and doable.

In my understanding the black diamonds just represent the "ownership", saying who is responsible for releasing memory and chaining the calls to destructors and invalidating the other weak pointers (those coming from lines with white diamonds and lines without diamonds)

Another picture where both diamonds co-exist at the same time can be found at Kirill Fakhroutdinov's site under..

EDIT: after @gwag comment http://www.uml-diagrams.org/library-domain-uml-class-diagram-example.html (see the Library class)

..http://www.uml-diagrams.org/dicom-real-world-uml-class-diagram-example.html (see the Visit class)




回答3:


Your main question ("are composites allowed to be shared with any other kind of relation in a uml class diagram") is not very well expressed. You probably refer to components and not to composites. Indeed, a wheel, as a part/component of a car, cannot be shared with other cars (at the same time), while an employee, as a part of a company or team, can be shared with several other companies or teams..

But this non-shareability concerns only the two classes involved in the composition relationship (Car and Wheel, Company and Employee). And it does not restrict component objects to participate in other relationships instantiating other associations.



来源:https://stackoverflow.com/questions/25227742/class-diagram-can-i-share-a-composition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!