Owner Class:
class University {
List<Student> studentCollection;
public void addStudent();
public void removeStudent();
}
owned Class:
class Student {
String name;
public void setName();
public String getName();
}
Here University is composed of Students and if we delete university object, all its students are supposed to be deleted.
But i want to ask the Student what University it is in?
Can we change Student class to have the reference of University?
Proposed class
class Student {
String name;
University univRef;
/* ... */
}
would this still be appropriate composition? In UML can this be marked as composition?
Or should it be changed to bidirectional association?
Note: Searching for the answer wasn’t successful & struggled with formatting 🙂
Yes, it is still a composition. The Students are still owned by the University and they are still killed when the University burns down.
Formally speaking, the first composition (without the back-reference), is modeled in a UML class diagram with a filled diamond on the University end, and an arrow on the Student end. The composition without the back-reference is then represented without the arrow. However, a lot of people are not very strict in applying the arrow, using the second notation also for a unidirectional composition.
To be clear you should model it with two arrows: One with a filled diamond from the University to the Student, the other without diamond from the Student to the University.