在java代码中实现UML三元关联

Sus*_*ha7 5 java oop uml ooad class-diagram

我目前在代码中实现三元关联时遇到一些麻烦。我得到了二进制关联,但我不确定三元关联。

这是大学里的典型场景。

讲师可以向一名或多名学生教授一门科目
学生只能由一名讲师教授一门科目
讲师只能向一名学生教授一门科目

这三个类之间存在三元关联。

这三个类之间的关系如下面的 UML 类图所示,并且存在多重性

在此输入图像描述

我在互联网上阅读了有关此问题的不同来源,但找不到解决方案

如何实现这三个类之间的关联?或者,一般来说,实现类之间关联的可能方法是什么(在java中)?

qwe*_*_so 3

一如既往,这取决于。

实现关联类的常用方法是使用关联数组。Lecturer在您的示例中,您(最终)将使用/的组合Subject来访问 s 列表Student。如果您的要求不同,您可以在Subject提供.StudentTeacher

当使用数据库时,您将在表中拥有Lecturer//主键。这允许像上面提到的那样进行个人选择。SubjectStudentTeaching

我的一些伪代码:

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
}
Run Code Online (Sandbox Code Playgroud)