Aggregation (object-oriented programming): Difference between revisions
No edit summary |
m Reverted edits by Principalityofgalore to last version by TakuyaMurata |
||
Line 1: | Line 1: | ||
{{merge|composition (computer science)|]] |
|||
PRINCIPALITY OF GALORE!!! |
|||
{{cleanup-verify}} |
|||
'''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. 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++]]: |
|||
<pre> |
|||
class Professor; |
|||
class Department |
|||
{ |
|||
... |
|||
private: |
|||
Professor faculty[20]; |
|||
... |
|||
}; |
|||
</pre> |
|||
In aggregation, the object may only contain a reference or pointer to the object: |
|||
<pre> |
|||
class Professor; |
|||
class Committee |
|||
{ |
|||
... |
|||
private: |
|||
Professor* members[5]; |
|||
... |
|||
}; |
|||
</pre> |
|||
{{Compu-stub}} |
|||
[[Category:Object-oriented programming]] |
Revision as of 09:23, 28 May 2005
{{merge|composition (computer science)|]]
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. 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]; ... };