Java Reference
In-Depth Information
An aggregation relationship is usually represented as a data field in the aggregating class.
For example, the relationships in Figure 10.6 may be implemented using the classes in
Figure 10.7. The relation “a student has a name” and “a student has an address” are imple-
mented in the data field
name
and
address
in the
Student
class.
public class
Name {
...
}
public class
Student {
private
Name name;
private
Address address;
...
}
public class
Address {
...
}
Aggregated class
Aggregating class
Aggregated class
F
IGURE
10.7
The composition relations are implemented using data fields in classes.
Aggregation may exist between objects of the same class. For example, a person may have a
supervisor. This is illustrated in Figure 10.8.
1
Person
1
Supervisor
F
IGURE
10.8
A person may have a supervisor.
In the relationship “a person has a supervisor,” a supervisor can be represented as a data
field in the
Person
class, as follows:
public class
Person {
// The type for the data is the class itself
private
Person supervisor;
...
}
If a person can have several supervisors, as shown in Figure 10.9a, you may use an array to
store supervisors, as shown in Figure 10.9b.
1
public class
Person {
...
private
Person[] supervisors;
Person
Supervisor
m
}
(a)
(b)
F
IGURE
10.9
A person can have several supervisors.
Note
Since aggregation and composition relationships are represented using classes in the
same way, we will not differentiate them and call both compositions for simplicity.
aggregation or composition
10.3
✓
✓
What are common relationships among classes?
Check
10.4
What is association? What is aggregation? What is composition?
Point
10.5
What is UML notation of aggregation and composition?
10.6
Why both aggregation and composition are together referred to as composition?
Search WWH ::
Custom Search