Monday, November 25, 2013

Association, Aggregation and Composition

Association, Aggregation and Composition are the relationship among classes in java.

This relationship can be derived as shown below:

[Association]--------->[Aggregation]--------->[Composition]

Association:

Association is a (*a*) relationship between two classes. It allows one object instance to cause another to perform an action on its behalf. Association is the more general term that define the relationship between two classes.

At an outline

i. Its an relationship between two objects.
ii. Define multiplicity between objects.
iii. The relationship may be one-to-one, one-to-many, many-to-many.
iv. It represents a binary relationship between two objects that describes an activity.

Example.

[STUDENT]-----takes----->[COURSE]<-------teaches------[TEACHER]


Aggregation:

Aggregation is the (*the*) relationship between two classes. When object of one class has an (*has*) object of another.

At an outline

i. Its a special kind of association.
ii. A directional association between objects.
iii. When an object "has a" another then you have got an aggregation between them.
iv. Direction should be specified between object, which object should contain which object.

Example.

[Department]  <------------- [Developer]
                                            [Java]
                                            [dot-net]

Let say we have a Department and Developer. The Developer can be a java or dot-net  developer and can be in a department. If we delete department object still the developer exists.


Composition:

Composition is a special type of aggregation design technique to implement has-a relation ship between classes.Basically its a technique for code reusability.

At an outline

i. Its a special case of Aggregation.
ii. A restricted aggregation is called composition.
iii. When an object contains other object.If the contained object cannot exist with out the existence of a container object, Its called a composition.

Example.

A class contain students. The students cannot exist without a class. There exist a composition between class and students.



No comments:

Post a Comment