Title: Aggregation
1Aggregation Composition
- Reference Course Notes p. C-17 to C-21.
- Aggregation
- Aggregation a special sort of association - the
part-of relationship. e.g. an engine is part-of a
motor vehicle. -
- or more generally when an object consists of a
collection of other objects This is a part-of a
whole relationship. - Write or say it to see if it makes sense e.g.
- A roof is part-of a house (aggregation).
- A mail message header is part-of a mail message,
a processor is part-of a computer etc - Don't confuse this with specialisation/generalisat
ion - e.g. A tile roof is-a type of roof rather than a
tile roof is part-of a roof
2Aggregation Composition
- Reference Course Notes p. C-17 to C-21.
- Aggregation
- Aggregation a special sort of association - the
part-of relationship. e.g. an engine is part-of a
motor vehicle. - Notation
This is read as Class1 contains Class2, or
alternatively Class2 is part-of Class1.
- Note these are associations, so multiplicities
may also apply. - This is not the same as inheritance, which is an
is-a relationship
3Aggregation Composition
An Engine is part-of a MotorVehicle.
Engine
MotorVehicle
An example object diagram
MotorVehicle
e1Engine
Example 2
The following class diagram means A Processor is
part-of a Motherboard. A MotherBoard is part-of
a Computer. A Computer has one Motherboard. A
Motherboard can have 1 to 4 Processors.
4Aggregation Composition
An example object diagram
5Aggregation Composition
- Example 3 Fig 6-35
- The following class diagram is for an email
message.
Description A mail message has the parts one
header, one body and zero or more attachments.
An attachment may belong to zero or more mail
messages.
6Aggregation Composition
- Example 3 Fig 6-35
- An example object diagram
Description A mail message has the parts one
header, one body and zero or more attachments.
An attachment may belong to zero or more mail
messages.
7Aggregation Composition
- The use of aggregation does not impose any extra
constraints (in terms of multiplicity) on the
model in addition to those already implied by the
use of an association. For example, the following
has the same multiplicity as the above. However
it provides additional information amount the
relationships e.g. the Header is part-of a
MailMessage.
8Aggregation Composition
- Properties of Aggregation
- The links instantiated from aggregation
associations obey certain rules. They are - transitive
- antisymmetric
-
- Transitivity means that if object A is part-of
object B and object B is part-of object C, then
object A can also be seen as part-of of object C. - i.e. taken from the following class diagram
Antisymmetry means that an object cannot be
part-of itself. (cannot have an aggregation self
association)
9Aggregation Composition
- When might aggregation be useful?
- Consider the following class diagram for part of
a stock control problem
The abstract class Component describes the
general idea of a component.
A Component can either be a single part or an
assembly consisting of several components.
However the class diagram as drawn permits a self
association for an assembly (i.e. an assembly
that is part of itself)
10Aggregation Composition
- For example, the following object diagrams are
legal
x Assembly
y Assembly
Part
z Assembly
Part
Assembly z is part of assembly y and assembly y
is part of assembly x!
The assembly is part of itself!
11Aggregation Composition
- The use of aggregation can remove these
problems (see pC-18 of the course notes ) i.e.
not allow an association between an assembly
object and itself OR the circular problem
12Aggregation Composition
- The use of aggregation can remove these
problems (see pC-18 of the course notes ) i.e.
not allow an association between an assembly
object and itself OR the circular problem
The previous two object diagrams are now illegal
y Assembly
x Assembly
z Assembly
Part
Part
Violates the property of antisymmetry due to
transitivity z is part of y and y is part of z
gt z is part of z, but this is not permitted!
Violates the property of antisymmetry
13Aggregation Composition
- Composition
- Composition is a stronger form of aggregation.
- the part is dependent on the whole
- the part cannot exist by itself
- when the object corresponding to the whole is
destroyed then the object(s) corresponding to the
part is (are) also destroyed - a part object can only belong to one whole object
(composite) at a time. - Notation
This is read as Class1 is composed of Class2.
14Aggregation Composition
- Composition an example
- We could model the mail message example using
composition. This will add extra constraints to
the model i.e.
This now says that when a MailMessage object is
destroyed, so are the Header object and the Body
object. The attachment object(s) are not
destroyed with the MailMessage object, but still
exist.
15Aggregation Composition
- A legal object diagram would be
But this is illegal
16Aggregation Composition
- Composition vs Aggregation
- When is composition used rather than aggregation?
This will depend on the problem - sometimes it is
appropriate that a part-of object exist after the
object it is a part-of is destroyed, other times
it makes no sense for it to continue to exist -
it may depend on business-rules, how the objects
are defined etc. - Example
- Two class diagrams are shown below, both can be
valid depending on the requirements. - The following class diagram may be for a garage
they have no interest in the engine once the car
it is in car ceases to exist, so an engine object
is destroyed when the car object it is part of is
destroyed.
17Aggregation Composition
- Composition vs Aggregation
- When is composition used rather than aggregation?
This will depend on the problem - sometimes it is
appropriate that a part-of object exist after the
object it is a part-of is destroyed, other times
it makes no sense for it to continue to exist -
it may depend on business-rules, how the objects
are defined etc. - Example
- Two class diagrams are shown below, both can be
valid depending on the requirements. - Whereas the following class diagram may be for
Vic Roads they wish to know the engine details
even if the car ceases to exist (e.g. it was in a
crash, but the engine was salvaged)
18Next time
- Association classes
- Qualified associations
- Further detail on the way you specify methods
(functionality) and data (attributes)