Aggregation (object-oriented programming): Difference between revisions
No edit summary |
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. |
||
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 is usually implemented such that an object contains another object. For example, in [[C plus plus|C++]]: |
|||
<pre> |
<pre> |
||
Line 19: | Line 19: | ||
</pre> |
</pre> |
||
In |
In aggregation, the object may only contain a reference or pointer to the object: |
||
<pre> |
<pre> |
Revision as of 13:16, 6 April 2005
This article needs additional citations for verification. |
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. 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 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]; ... };