Was reading UML Distilled over safaribooksonline, tried hard but couldn't determine it clearly explains difference between aggregation and association, so decided to ask couple of gurus i know. Here are 2 replies
---------------------------------------------
Mat reply
----------
Hey. Its a very fine conceptual difference, and I don't think anyone would
fault you for using one in place of the other.
The rule of thumb is, associations are graphs whereas aggregations are trees.
Think of it this way:
An association is a two-way possession (or perhaps more complicated
reference which is cyclic though not immediately visible as such). A
husband object will have an association to his wife object, for example,
and the wife would have an association to her husband.
An aggregation is strictly a one-way possession. A simple example here is
a ArrayList of String objects. The String cannot have a reference to the
ArrayList or a reference to any other Object with a reference to the
ArrayList so the ArrayList has a aggregation of many Strings.
This brings us to the last reference type, composition.
These are basically references to "parts of" the greater object which need
not be private. In UML it is supposed to be treated the same as
aggregation, though in practice, I've seen way more associations than
aggregations come out of a final product. The key difference is that the
lifetime of aggregates is governed by the lifetime of the parent. The
example here is a hand object consists of a composition of 5 fingers. If
you lose the hand, you lose the fingers too.
Note that this harder to pull off in Java because there are no explicit
call to "free" so unless the member is private, its possible to hold a live
reference to a finger from a hand that has been destroyed--it does help to
think about who is referencing what, and how they are getting their references.
---------------------------------------------
Vguru reply
------------
Huu.. Good question to ask,
If u want to implement this aggregation is like array of objects and
association is signle element,
in java terms.
public class C {
private DependentObject[] members; // is aggregation
private DependentObject member ; // this is association.
}
In general, During Class design time u need to think of how does the
co-ordination is required with other classes.
Remember the life of any of these DO(s)are not actually with Class
'C', which meen, There are externally created and 'C' will use those
instaces. may be the array of DO (dependentObject) will come eaither
from the Constructor or some method, but they will exist after the
scope of instance C, Doesn't meen some other object will also cache
these references and they exist further life of the C. The creation
should not happen in C in such a case that should become a
containment.
The other way to find same scope is when u design a Class diagram and
decide on the classes and their behavior with CRC (Class Resposibility
Card), they these two objects C and X will have same scope, scope here
represents the preception of resposibility they do in the system, and
some functionality of one will requirea handshake with other or others
for which the relation between the classes is aggregartion and
association , dependency or containment are defined.
In the life time of C and X, there should not have any comparisions.
they both are indepndent , The more independent is the less dependent
or handshake with other objects, so desin principle is to achive mode
independent objects. want to make more indepent, then u will be
designing interfaces, for other less dependenting that reduces
dependency and provideds flexibility.
The more interfaces and abstract classes it has is more flexible or
easy to write a pluggable code to that framework or extensible.
Ofcoure there are some syntactical elements of above, which can change
depending on the language which u choose to implement ur design.
----------