I'm currently having some trouble implementing ternary associations in Code. I get the binary ones, but I am unsure about ternary Associations.
this is typical scenario in a university.
Lecturer can teach a one subject to one or more students
student can teach one subject from only one Lecturer
Lecturer can teach one student to only one subject
There exists a ternary association between these three classes.
Relation among these three classes shown in below UML class diagram and also multiplicities are there
I've read on different sources regarding this all across the internet and coudn't find a solution
How do I implement the association between these three classes ? or, In general What are the possible ways to implement association between classes (in java) ?
As always, it depends.
The usual way to implement association classes is by using associative arrays. In your example you'd have (eventually) a combination of Lecturer
/Subject
to access a list of Student
s. If your requirements are different you could for example return a list of Subject
/Student
s when supplying a Teacher
.
When using a database you will have the primary keys of Lecturer
/Subject
/Student
in a table Teaching
. This allows for individual selects like for the ones mentioned above.
Some pseudo code out of my hat:
class Teaching {
private Hash lectRef; // assoc. array
public void addTeaching(Lecturer lect, Student stud, Subject subj) {
if lectRef[lect.hash] == None { lectRef[lect.hash] = []; }
// if none, default an empty array here
// lect.hash is a unique hash code for the Lecturer object
lectRef[lect.hash].append((stud, subj);
// tuple of student/subject referenced
// if you need other fast results (e.g. subjects per student or the like) you need to hash them here too
}
public [(Stud, Subj)] studSubj (Lecturer lect) {
return lectRef[lect.hash];
// returns the array of student/subject tuples
}
// add other result operations like subjects per student as needed
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With