Is my relationships correct in my class diagram

88 views Asked by At

Im trying to figure out if the relationships between each class is correct. Im trying to learn it and get confused between what each should be.

The is the code for each:

package part1;
public enum Role {
    STUDENT, ACADEMIC
}

package part1;
import part1.part11.Students;
import part1.part12.AcademicStaffMembers;
public class Controller {
    private int id;
    public Controller(Role role) {
        if(role == Role.ACADEMIC){
            AcademicStaffMembers academicStaffMembers=new AcademicStaffMembers();
        }
        else if(role == Role.STUDENT) {
            Students students = new Students();
        }
    }
}

package part1.part11;
public class Students{
    private Student[] students;
}

package part1.part11;
import part2.Module;
import part2.ModuleCode;
public class Student {
    private int id;
    private Module[] modules;
    private ModuleCode[] moduleCodes;
}

package part1.part12;
public class AcademicStaffMembers {
    private AcademicStaffMember[] academicStaffMembers;
}

package part1.part12;
import part2.Module;
import part2.ModuleCode;
public class AcademicStaffMember {
    private int id;
    private Module[] modules;
    private ModuleCode[] moduleCodes;
}

package part2;
public class Module{
    private ModuleCode moduleCode;
}

package part2;
public enum ModuleCode{
    CSC1022, CSC1023, CSC1024, CSC1025, CSC1026, CSC1027, CSC1028, CSC1029, CSC1030, CSC1031
}

Currently I believe the relations are as follows:

Controller:

  • Association with Role
  • Dependency with Students
  • Dependency with AcademicStaffMembers

Students

  • Composition with Student (Diamond at Students)

AcademicStaffMembers

  • Composition with AcademicStaffMember (Diamond at AcademicStaffMembers)

Student

  • Association with Module
  • Association with ModuleCode

AcademicStaffMember

  • Association with Module
  • Association with ModuleCode

Module

  • Composition with ModuleCode (Diamond at Module)

UML Class diagram

Any help or advice would be amazing, thanks you.

1

There are 1 answers

0
Christophe On

Normally, we don't do reviews here.

However, your example is interesting in view of the confusion between transient relationships for the time of an operation and semantic relationships:

  • The relationship between Controller and Role is transient: it exists only temporarily for the time of an operation, even if the operation is the constructor. It's not an association between classes, but a dependency.

    This shows by the way a design flaw, since students and acedemicStaffMembers collections are created during the operation, but cannot be used outside that operation, making them somewhat useless. To make it work, both Students and AdademicStaffMembers should be singleton classes, or both students and acedemicStaffMembers should be made private attributes of Controller.

It is also interesting for the use of composite aggregation (black diamond) for modelling collections:

  • In UML enumerations are value objects. It makes no sense to add composite aggregation for an enumeration in this context: prefer a simple association.
  • It is not wrong for collections, but means that the students and staff members in these collections are destroyed if composite is destroyed. This is not trivial in Java, since reference to the students and staff members may exist elsewhere, preventing the destruction of those objects. I'd recommend to prefer using a simple association with explicit multiplicities 0..1 on the side of the collection, and * on the elements to be collected, which is far more useful and accurate. Consider using composite aggregation only if strictly needed.

Showing multiplicities would also significantly improve the expressiveness of your diagram (for example to clarify that academic staff members can be associated with many modules and many module codes).

Additional advice: The (ab)use of enumeration should raise some questions:

  • is the purpose of the enum to change behavior (as for the Controller)? If yes, consider specialisation - It makes perfect sense to have a StudentController and a AcademicStaffMemberController inheriting from a more general Controller, since the behaviours, verifications, rules, operations might not be the same for the two.
  • is the purpose of the enum to serve as a list of data (as for ModuleCode)? If yes, keep them only if the list is supposed to be fixed for a long time. If the list may however change (e.g. new course modules could be introduced every year, like CS1032 for the new VR UX, or CS1033 for Latest developments in LLM AI) then consider moving away from enum and use ModuleCodes as a class with attributes code and title.