Jump to content

Aggregation (object-oriented programming): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
The terms composition and aggregation should be switched. Composition causes the contained objects to be destroyed if the container is destroyed. In aggregation, the objects may not be destroyed.
No edit summary
Line 3: Line 3:
'''Aggregation''' is a form of [[Composition (computer science)|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 [[Object (computer science)|objects]] of other [[Class (computer science)|classes]] inside the class. An example will be a [[Automobile|car]] class which is an aggregation of different classes like [[wheel]], [[engine]], [[Car body|body]] etc.
'''Aggregation''' is a form of [[Composition (computer science)|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 [[Object (computer science)|objects]] of other [[Class (computer science)|classes]] inside the class. An example will be a [[Automobile|car]] class which is an aggregation of different classes like [[wheel]], [[engine]], [[Car body|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. These relationships can be modelled as composition since if the university disbands the departments certainly will, and if a department disbands, the professors in that department will no longer have jobs at the university (and no longer exist in this scope). On the other hand, suppose several professors are on a committee. This relationship is best modelled as an aggregation—but not a composition—since the disbanding of the committee would not mean the professors lost their jobs.
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 plus plus|C++]]:
Composition is usually implemented such that an object contains another object. For example, in [[C plus plus|C++]]:

Revision as of 06:49, 16 May 2005

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];
  ...
};