Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Impliment UML Ternary Association in java Code

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

enter image description here

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) ?

like image 314
Susantha7 Avatar asked Oct 15 '18 08:10

Susantha7


1 Answers

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 Students. If your requirements are different you could for example return a list of Subject/Students 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
}
like image 172
qwerty_so Avatar answered Sep 18 '22 19:09

qwerty_so