Java Reference
In-Depth Information
relation “a student takes a course” is implemented using the addCourse method in the Stu-
dent class and the addStuent method in the Course class. The relation “a faculty teaches
a course” is implemented using the addCourse method in the Faculty class and the set-
Faculty method in the Course class. The Student class may use a list to store the courses
that the student is taking, the Faculty class may use a list to store the courses that the faculty
is teaching, and the Course class may use a list to store students enrolled in the course and a
data field to store the instructor who teaches the course.
public class Student {
private Course[]
courseList;
public class Course {
private Student[]
classList;
private Faculty faculty;
public class Faculty {
private Course[]
courseList;
public void addCourse(
Course s) { ... }
}
public void addCourse(
Course c) { ... }
}
public void addStudent(
Student s) { ... }
public void setFaculty(
Faculty faculty) { ... }
}
F IGURE 10.5
The association relations are implemented using data fields and methods in classes.
Note
There are many possible ways to implement relationships. For example, the student
and faculty information in the Course class can be omitted, since they are already in
the Student and Faculty class. Likewise, if you don't need to know the courses a
student takes or a faculty member teaches, the data field courseList and the add-
Course method in Student or Faculty can be omitted.
many possible
implementations
10.4.2 Aggregation and Composition
Aggregation is a special form of association that represents an ownership relationship between
two objects. Aggregation models has-a relationships. The owner object is called an aggregating
object , and its class is called an aggregating class . The subject object is called an aggregated
object , and its class is called an aggregated class .
An object can be owned by several other aggregating objects. If an object is exclusively
owned by an aggregating object, the relationship between the object and its aggregating object
is referred to as a composition . For example, “a student has a name” is a composition relation-
ship between the Student class and the Name class, whereas “a student has an address” is an
aggregation relationship between the Student class and the Address class, since an address
can be shared by several students. In UML, a filled diamond is attached to an aggregating
class (in this case, Student ) to denote the composition relationship with an aggregated class
( Name ), and an empty diamond is attached to an aggregating class ( Student ) to denote the
aggregation relationship with an aggregated class ( Address ), as shown in Figure 10.6.
aggregation
aggregating object
aggregated object
aggregated class
aggregating class
composition
Composition
Aggregation
1
1
1..3
1
Name
Address
Student
F IGURE 10.6
Each student has a name and an address.
In Figure 10.6, each student has only one multiplicity—address—and each address can
be shared by up to 3 students. Each student has one name, and a name is unique for each
student.
 
 
Search WWH ::




Custom Search