Originally posted by Kalpesh Soni:
Association -
Aggregation -
Composition -
You aren't alone.
Association is simply when one class contains a variable that holds a reference to another:
<pre>
class A {
private B itsB;
}
|A|----------->|B|
itsB
</pre>
Aggregation is exactly the same thing, except that instances related by aggregation cannot aggregate themselves. i.e. There can be no cycles in the instance graph. i.e. An aggregate cannot be its own aggregee.
This distinction is so narrow that I have given up ever using aggregation. There is virtually no way to identify aggregation by looking at the source code. I also find that because nobody realy knows what aggregation is, everyone uses it for lots of different things.
Composition is used when one object is responsible for the lifetime of another. This is more common in C++ than in Java. In Java, the garbage collector has all the lifetime responsibility. In C++ certain classes are responsible for seeing to it that certain objects are destroyed. Those classes have Composition relationships to the classes of the destroyed objects.
<pre>
class A {
private:
B* itsB;
public:
A() {itsB = new B();}
virtual ~A() {delete itsB;}
};
|A|<#>--------->|B|
itsB
</pre>