标签云

微信群

扫码加入我们

WeChat QR Code

What is the difference between association, aggregation, and composition? Please explain in terms of implementation.


This may be a tad overwhelming for a beginner, but take a wee stroll through the UML 2 Superstructure spec: omg.org/docs/formal/09-02-02.pdfSection 7.3.3 for Association

2019年04月19日31分23秒

I should also add, in UML 2 there is no such Element as Aggregation or Composition (it was in UML 1.4 though).In UML 2, aggregation/compositions are implemented as Association elements with the AggregationKind property set to either Shared or Composite.

2019年04月18日31分23秒

Lots of answers on SO already: stackoverflow.com/search?q=aggregation+and+composition

2019年04月19日31分23秒

useful article here codeproject.com/Articles/22769/…

2019年04月18日31分23秒

I know this has already been answered many times, but I feel the best explanation I've ever read on the matter is this one: holub.com/goodies/uml/#composition

2019年04月19日31分23秒

Seems C#/Java code. If that's the case, both Association and Aggregation code are same. In both cases, 'bar' is just referenced and Bar object may live on.

2019年04月18日31分23秒

Jeff Foster: I have some doubts. If I instantiate bar object in Baz(Bar bar){bar=new Bar(); } in first case. Will it still be association or will it become composition now?

2019年04月18日31分23秒

Ajay: Aggregation keeps the reference of the objects which is not the case with association. Hence the difference of implementation.

2019年04月19日31分23秒

Association is a bit stronger than just usage as a method parameter. I believe your association code snippet corresponds more to a Dependency relation. you might want to check Martin Fowler related article

2019年04月19日31分23秒

AhmadAbdelghany is correct. First example is a dependency relation. The third one works for association and aggregation.

2019年04月19日31分23秒

I was going to ask why you cared to answer an already answered question that was asked more than 5 years ago but then I read your blog entry and it was way more informative than some of the answers here. Upvoted!

2019年04月19日31分23秒

I agree with Donbhupi your answer is way more informative and correct than many others

2019年04月18日31分23秒

It's really funny when C# and Java developers claim they use composition when it only exists on primitive types with those languages. If you want to really understand composition you have to use C++ where objects can REALLY be part of other objects.. Not just floating in heap memory and holding pointers to each other and claiming there is composition..

2019年04月18日31分23秒

Everyone i arrived at the same conclusion as you, but i'm not so sure of it. For example, say i have one class that is semantically owned by one specific class, however the owned object is garbage collected after its owner has already been removed by the garbage collector, is it considered a composition?

2019年04月19日31分23秒

Can we have composition within a c# code using managed memory?

2019年04月18日31分23秒

After reading so much on this topic, this answer is the most intuitive understandable one. Should be put on wikipedia.

2019年04月18日31分23秒

Beautifully articulated.

2019年04月19日31分23秒

Nicely explained...

2019年04月18日31分23秒

How much authority does this definition have?Is it supported by the UML standard authors?I it supported by tools?

2019年04月18日31分23秒

It's the Robert C. Martin. That's enough authority for me :-)

2019年04月18日31分23秒

This should be the only accepted answer. Composition does not exist in C# and Java except with primitive types... Yet you see developers of those languages "explaining" composition. Composition means an object exists INSIDE another. In Java and C# you cant even do it, everything is on the heap and you just hold pointer to it, it is really aggregation not composition. C++ provides composition..

2019年04月19日31分23秒

upvote for the incomplete list of repeatedly asked questions.

2019年04月19日31分23秒

Whoever has down voted this answer. Can you please explain reason for down voting it ?

2019年04月18日31分23秒

What really confuses me is that, in many cases, it is not the owner hold the thing, but the thing it owns "hold" the owner. For example, the car does not have a Engine * type pointer, but the Engine class has a Car type member to store the car that owns it. I don't quite understand it especially the uml relationship of the classes in this case.

2019年04月19日31分23秒

Strictly speaking, employees of a company cannot exist without a company. It is true, you don't kill the people, but they are no longer employees of that company. So I think a better analogy would be with a Branch and Employees, where even if the branch closes, they might continue to be employees of the company.

2019年04月18日31分23秒

yup, absolutely. Do agree... +1 Thanks AlexPopov for pointing it out. :)

2019年04月18日31分23秒