Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UML: how to implement Association class in Java

Tags:

I have this UML Association class. Note that: horizontal line is a solid line and the vertical line is a dashed line.

 ---------                  --------- |         |*(a)        *(b)|         | | CLASS   |________________|  CLASS  | |STUDENT  |     |          |  COURSE |  ---------      |           ---------                 |*(c)           ______|______          |             |          |             |          |  CLASS      |          | TRANSCRIPT  |          |_____________| 

I understand this relationship but I have met some problems when implement this UML to code. I can implement relation between class Student and class Course to code. Here is my code:

class Student {   Vector<Course> b; }  class Course {    Vector<Student> a; } 

But, at class Transcript, I don't understand so much, how to use this class in code. Is it the property of both class Student and Course. So, if that's true then the code will be:

class Student {   Vector<Course> b;   Vector<Transcript> c; }  class Course {   Vector<Student> a;   Vector<Transcript> c; } 

Is it true? If this is wrong, please teach me how to implement this UML.

Thanks :)

like image 695
hqt Avatar asked Nov 18 '12 19:11

hqt


People also ask

What is UML association in Java?

An "association" in UML is defined as a kind of relationship between classes1, which represents the semantic relationship between two or more classes that involves connections (links) among their instances [UML, p. 2-20]2.

How do you implement an association?

Associations can be described as a "has-a" relationship because the typical implementation in Java is through the use of an instance field. The relationship can be bi-directional with each class holding a reference to the other. Aggregation and composition are types of association relationships.

How can you use association as a class?

An association class is identical to other classes and can contain operations, attributes, as well as other associations. For example, a class called Student represents a student and has an association with a class called Course, which represents an educational course. The Student class can enroll in a course.


2 Answers

First of all, don't use Vector, as it's an old class that shouldn't be used anymore for more than 10 years. Use either a Set or a List.

If the Transcript class contains information about the way a student attends a course (for example, the date of its subscription to the course), you could implement it like this:

class Student {     Set<Transcript> transcripts; }  class Transcript {     Student student;     Course course;     Date subscriptionDate; }  class Course {     Set<Transcript> transcripts; } 

That doesn't prevent you from providing a method in Student that returns all his courses: 

public Set<Course> getCourses() {     Set<Course> result = new HashSet<Course>();     for (Transcript transcript : transcripts) {         result.add(transcript.getCourse());     }     return result; } 

If Transcript doesn't contain any information, then it's probably there to model how these classes would be mapped in database tables, where the only way to have a many-to-many association between two tables is to use a join table holding the IDs of the two associated tables.

like image 112
JB Nizet Avatar answered Sep 22 '22 07:09

JB Nizet


I know this question is very old but I have a way more convenient than embedding n..1 relations in the association class :

public class Transcript {     //Transcript's properties }   public class Course {      private Map<Student, Transcript> transcriptsByStudent; }  public class Student {      private Map<Course, Transcript> transcriptsByCourse; } 
like image 27
Aleks Ben Maza Avatar answered Sep 18 '22 07:09

Aleks Ben Maza