tags:

views:

544

answers:

4

What is the difference between aggregation, composition and dependency?

+3  A: 

aggregation and composition are almost completely identical except that composition is used when the life of the child is completely controlled by the parent.

Aggregation

Car->Tires

The tires can be taken off of the car object and installed on a different one. Also, if the car gets totaled, the tires do not necessarily have to be destroyed.

Composition

Body->Blood Cell

When the Body object is destroyed the BloodCells get destroyed with it.

Dependency

A relationship between two objects where changing one may affect the other.

Robert Greiner
Funny, I just read a tutorial where the car-tires example is used to illustrate composition...
mouviciel
interesting, I guess it depends on how you look at it. I don't see how destroying the car object also mandates that the tires be destroyed as well. Also, you can take the tires off of a car and put them on a different car. That's the problem with analogies though I suppose.
Robert Greiner
Yes, indeed I understood aggregation and composition not with such analogies but with actual code.
mouviciel
+3  A: 

Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist.

Composition implies a relationship where the child cannot exist independent of the parent. Example: House (parent) and Room (child). Rooms don't exist separate to a child.

The above two are forms of containment (hence the parent-child relationships).

Dependency is a weaker form of relationship and in code terms indicates that a class uses another by parameter or return type.

Dependency is a form of association.

cletus
Mention the fact that aggregation and composition are specialization of the containment relationship form and it would be perfect.
:-) Not saying you are right or wrong, but gees these classifications suck. Does Class/Student == Aggregation?... Not according to JavaPapers.com. http://javapapers.com/oops/association-aggregation-composition-abstraction-generalization-realization-dependency/ "A class contains students. A student cannot exist without a class. There exists composition between class and students."
TallPaul
Btw, personally I agree with you...
TallPaul
@tallPaul the paper you mention agrees with my definition of aggregation and composition. It simply uses a different definition for student. It says student cannot exist without class. If that's the case then yes it is composition. If not, it's aggregation. I don't like their premise: students can exist and not be in any classes.
cletus
A: 

An object associated with a composition relationship will not exist outside the containing object. Examples are an Appointment and the owner (a Person) or a Calendar; a TestResult and a Patient.

On the other hand, an object that is aggregated by a containing object can exist outside that containing object. Examples are a Door and a House; an Employee and a Department.

A dependency relates to collaboration or delegation, where an object requests services from another object and is therefor dependent on that object. As the client of the service, you want the service interface to remain constant, even if future services are offered.

Mark
A: 

Aggregation and composition are terms that most people in the OO world have acquired via UML. And UML does a very poor job at defining these terms, as has been demonstrated by, for example, Henderson-Sellers and Barbier ("What is This Thing Called Aggregation?", "Formalization of the Whole-Part Relationship in the Unified Modeling Language"). I don't think that a coherent definition of aggregation and composition can be given if you are interested in being UML-compliant. I suggest you look at the cited works.

Regarding dependency, that's a highly abstract relationship between types (not objects) that can mean almost anything.

CesarGon