Last sentence should be: A Text Editor owns a Buffer(composition). A Text Editor uses a File (aggregation). When you apply this correction, I will change my stance on your post from negative (confusing) to positive. :)
–
Ed JamesMar 25 '11 at 8:37

3

So is a car an aggregate or a composition of its parts?
–
reinierpostMar 31 '11 at 12:13

1

And how is aggregation different from any other relationship between two sorts of entities?
–
reinierpostMar 31 '11 at 12:14

13

@reinierpost In reality, a car is an aggregation of parts, and parts are simply an aggregation of molecules... However, in a model it all depends on your requirements. Is it important to treat the engine as a separate entity so that you can track its lifetime independent of the car? Can you reuse the exact same engine in another car? If so, then you probably want aggregation. Otherwise you want a composition because you don't care about engines that aren't part of cars, nor do you care about reusing engines.
–
Curtis BattMar 31 '11 at 14:06

1

what is missing is an implementation example for a complete understanding...
–
Chesnokov YuriyOct 26 '13 at 10:04

An array of objects is a composition.
An array of pointers to objects is an aggregation.

If I delete the first one, its contents vanish with it.
The second one, on the other hand can vanish without affecting its members existence unless there is a specific method that deletes each object as its pointer is deleted.

The aggregation is when those subsets exists independently of the father set. As a monitor can be unplugged from the computer to be connected to another.

The composition is when those subsets depends of the existence of the father set. As a leaf is a part of a tree or liver is a part of a body.

These concepts talks about the kind of dependency between two objects or classes, conceptually. Directly in a program, in an aggregation, when the father object disposes, the aggregate objects should be disposed too. In the same scenario for a composition, composite son objects will persist then the father object dispenses.

Aggregation differs from ordinary composition in that it does not imply ownership. In composition, when the owning object is destroyed, so are the contained objects. In aggregation, this is not necessarily true. For example, a university owns various departments (e.g., chemistry), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.

So - while you have an ownership relationship with composition the owned object is also destroyed when the owner is - an aggregation (and the objects contained) can exist independently.

In the example you quote the composition is a one-to-many and the aggregation also has a one-to-many relationship implied, though here it could also be a many-to-many relationship for the aggregation (we can suppose possible that a teacher can teach in multiple departments). Whereas a department cannot be part of multiple universities. Composition implies ownership whereas aggregation does not go beyond relationship. The quote is correct but the comment is not.
–
NewtopianMar 24 '11 at 5:20

1

it has nothing to do with destruction! UML does not define garbage collection system.
–
Display NameMar 24 '11 at 10:01

1

i think the wikipedia link is getting reflexive upvotes, but this is a terrible definition - as @bold pointed out these relationships have nothing to do with GC. This also falls apart when an object is the component of two other objects, such as the ball in a ball-joint joining two artificial limbs. The Component relationship is about functional dependence.
–
Steven A. LoweMar 24 '11 at 14:13

1

I agree that my answer is severely lacking - but so is the WikiPedia article...
–
HorusKolMar 24 '11 at 22:13

I always look at composition as 'needs a', i.e. a car needs an engine, and I look at aggregation as 'things related for a purpose'. So staying with the car analogy, my aggregation may be to represent a journey which may involve bringing a car and passengers together. The journey does not own the car or the passengers, I'm aggregating data that is related for a specific scenario. When the journey is completed the car and the passengers go on. When a car is ended, the car and it's engine are normally destroyed together.