【问题标题】:Impliment UML Ternary Association in java Code在java代码中实现UML三元关联
【发布时间】:2018-10-15 08:46:48
【问题描述】:

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

这是大学里的典型场景。

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

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

这三个类之间的关系如下UML类图所示,也有多重性

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

如何实现这三个类之间的关联?要么, 一般来说,有哪些可能的方式来实现类之间的关联(在 java 中)?

【问题讨论】:

    标签: java oop uml class-diagram ooad


    【解决方案1】:

    一如既往,视情况而定。

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

    使用数据库时,Teaching 表中的主键为 Lecturer/Subject/Student。这允许像上面提到的那样进行个人选择。

    一些伪代码:

    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
    }
    

    【讨论】:

    • 感谢您提供示例代码(虚拟代码)用于向学生教授主题的讲座。教意味着将学科类分配给学生类中的类变量learnedSubjectList
    【解决方案2】:

    根据阿马丹 我正在准备一个答案

    由于我是 java 新手,语法可能不正确 如果这个解决方案是错误的,请纠正我

    //Lecturer class
    class Lecturer{
        private List<Klass> klassList; 
    
        public List<Klass> getKlasses(Klass klassList){
            retrun klassList;
        }
    
        public List<Klass> addKlasses(Klass klassList){
            this.klassList = klassList;
        }
    
        teachStudents(){
            klassList = getKlasses()
    
            for (int i = 0; i < klassList.size(); i++) {
                (klassList.get(i)).teachSubjectToStudent();
            }
        }
    }
    
    //Student class
    class Student{
        private Klass klass;
        private List<Subject> learnedSubjectList; 
    
        public void learn(Subject subject){
            learnedSubjectList.add(subject);
        }
    }
    
    //Subject class
    class Subject{
        private Klass klass;
    }
    
    //Klass class
    class Klass{
        private Lecturer lecturer;
        private Student student;
        private Subject subject;
    
        Klass(Lecturer lecturer,Student student,Subject subject){
            private this.lecturer = lecturer;
            private this.student  = student;
            private this.subject  = subject;
        }
    
        //implimentation of how subject was teach to a student
        public void teachSubjectToStudent(){
            this.student.learn(subject)
        }
    }
    
    //this class is use for code execution
    public class TestA {
        public static void main(String[] args) {
            Lecturer lecturer = new Lecturer();
            Subject subject   = new Subject();
    
            Student student1   = new Student();
            Student student2   = new Student();
            Student student3   = new Student();
    
            Klass klass1 = new Klass(lecturer,student1,subject);
            Klass klass2 = new Klass(lecturer,student2,subject);
            Klass klass3 = new Klass(lecturer,student3,subject);
    
            List<Klass> list = new ArrayList<Klass>();
            list.add(klass1);
            list.add(klass2);
            list.add(klass3);
    
            //create association link between lecturer class and klass class list
            lecturer.addKlasses(list);
        }
    }
    

    【讨论】:

      【解决方案3】:

      通常,您会将关联本身建模为一个实体(不能将其类称为 Class,因为 java.lang.Class 已经采用该类;通常人们将此类命名为 Klass,如很明显它指的是什么;如果你愿意,你也可以使用你的Teaching)。那么你就只有KlassTeacherKlassSubjectKlassStudent之间的二元关联。在这种情况下,TeacherStudent 根本没有关联,除非通过它们与 Klass 间接参与(与其他对类似)。

      【讨论】:

      • 感谢您可以提供专门用于Lectureteach() 方法的示例代码(虚拟代码)以及位于klass 类中的委托代码
      • 不,我们从来没有谈论过Lecture 类(对我来说,这意味着一个特定的时间段,其中涉及一个Klass 的所有实体都聚集在一起,但这只是我),我不知道teach 方法应该做什么(我不认为我可以做出令人信服的人工智能,让学生在 Stack Overflow 上听五分钟:P)。我一般说的是在现实生活中如何处理非二元关系。但我在问题中没有足够的细节来提供任何代码。
      • Lecture 是教书的。因此Lecture 班必须有Teaching() 方法我是正确的
      • 我以为是Lecturer
      • sorry.. Lecturer 是做教学的人。因此 Lecturer 类必须有 Teaching() 方法我是正确的
      猜你喜欢
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 2016-01-07
      • 1970-01-01
      相关资源
      最近更新 更多