Objects rarely work alone. A program is a group of objects that know about each other and work together. Unit 4 is about the different strengths of these links, starting with the loosest: association.
Association is a general relationship where one object uses or knows about another. Both objects have independent lifetimes; neither owns the other.
Examples: a Teacher teaches Students; a Doctor treats Patients; a Customer places Orders.
Multiplicity (cardinality): one-to-one (1:1), one-to-many (1:N), many-to-many (M:N).
class Teacher {
private String name;
Teacher(String name) { this.name = name; }
String getName() { return name; }
}
class Student {
private String name;
Student(String name) { this.name = name; }
String getName() { return name; }
// association: a student is taught by a teacher
void attend(Teacher t) {
System.out.println(name + " attends the class of " + t.getName());
}
}
Teacher t = new Teacher("Ms. Perera");
Student s = new Student("Nimal");
s.attend(t);If the student object disappears, the teacher object continues to exist, and the other way round.
Association in code: keeping a reference
The example above passes the Teacher into one method call. Strictly, UML calls such a short, temporary link a dependency (Student uses a Teacher for a moment). A full association is usually shown in code by one object keeping a reference to the other in an attribute, so that the link lasts:
class Student {
private String name;
private Teacher classTeacher; // association: a lasting link
Student(String name) { this.name = name; }
void setClassTeacher(Teacher t) { this.classTeacher = t; }
void showTeacher() {
System.out.println(name + "'s class teacher is " + classTeacher.getName());
}
}The Teacher is still created outside the Student and can be shared by many students. Setting a student's teacher does not create or destroy anybody.
Direction
- One-way (unidirectional): only one side knows about the other. A Student knows its Teacher, but the Teacher class has no list of students. In UML, draw an arrow head pointing at the class that is known.
- Two-way (bidirectional): each side keeps a reference to the other: a Teacher has a list of Students and each Student knows its Teacher. Drawn as a plain line. Two-way links are more work, because both sides must be kept in step.
Use one-way links unless the program really needs to navigate both ways.
Multiplicity in code
| Multiplicity | Meaning | Typical Java |
|---|---|---|
| 1 to 1 | A Person has one Passport | private Passport passport; |
| 1 to many | A Teacher teaches many Students | private List<Student> students = new ArrayList<>(); |
| Many to many | Students take many Courses; Courses have many Students | A list on each side, or a separate Enrolment class |
For many-to-many links, a separate class is often the cleanest design, because the link itself usually has data: an Enrolment can record the date and the grade for one student on one course.
Remember
- Association: objects know about or use each other, but neither owns the other.
- Both objects are created separately and have independent lifetimes.
- Multiplicity says how many objects are on each end: 1:1, 1:N or M:N.