Saturday, August 13, 2011

Using Encapsulation - via Association, Aggregation and Composition

That idea of encapsulation is to hide how a class does it but to allow requesting what to do.

According to Object Oriented Programming there are several techniques, classes can use to link with each other and they are named association, aggregation, and composition.

Association is a (*a*) relationship between two classes, where one class use another.

public class StudentRegistrar
public StudentRegistrar ();
new RecordManager().Initialize();

There is a directional association from StudentRegistrar to RecordManager or StudentRegistrar use a (*Use*) RecordManager.

Aggregation is the (*the*) relationship between two classes.

public class University
private Chancellor universityChancellor = new Chancellor();

In this case I can say that University aggregate Chancellor or University has an (*has-a*) Chancellor. But even without a Chancellor a University can exists.


Faculties cannot exist without the University, the life time of aFaculty (or Faculties) attached with the life time of the University . If University is disposed the Faculties will not exist. In that case we called that University is composed of Faculties. So that composition can be recognized as a special type of an aggregation.

as another example, you can say that, there is a composite relationship in-between aKeyValuePairCollection and a KeyValuePair

we can say that aggregation is a special kind of an association and composition is a special kind of an aggregation. (Association->Aggregation->Composition)

No comments:

Post a Comment