Jump to content

Aggregation (object-oriented programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 217.206.30.28 (talk) at 06:49, 16 May 2005. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Aggregation is a form of composition where one looks at the system as a whole rather than as parts. It is also based on has-a relationship and is implemented by creating objects of other classes inside the class. An example will be a car class which is an aggregation of different classes like wheel, engine, body etc.

Composition differs from aggregation in that composition indicates ownership. That is, in composition, when the containing object is destroyed, so are the contained objects. In aggregation, this is not necessarily true. For example, a university consists of various departments (e.g., chemistry), and each department contains a number of professors. If the university closes the departments will no longer exist, but the professors in those departments will. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors.

Composition is usually implemented such that an object contains another object. For example, in C++:

class Professor;

class Department
{
  ...
  private:
  Professor faculty[20];
  ...
};

In aggregation, the object may only contain a reference or pointer to the object:

class Professor;

class Committee
{
  ...
  private:
  Professor* members[5];
  ...
};