First of all, just as a suggestion to facilitate understanding of your model, I suggest you call your class "Discipline". Calling "Class" is confusing because they are all classes (in OO terms) and "Matter" are actually the subjects covered in a discipline.
Evaluating the scenarios you proposed:
1) If you use a list of Disciplines as a class attribute Student, you delegate to the Student class any change in your Disciplines. In that case, you would be applying the concept of Encapsulation, that is, the responsibility for knowing the details of the implementation would be hidden in the Student class.
Answer the following questions to find out if the above approach meets your use case:
What are the business rules applicable to the use case? For example, there is a minimum/maximum number of students per discipline, or a minimum/maximum number of subjects per student?
There are disciplines that have other as predecessors, being necessary to consult if that Student has already attended all the predecessors before enrolling it?
In these examples, Student class methods would be responsible for this control, regardless of whether another class is changing disciplines to that student.
Therefore, remember that objects have data (attributes) and behavior (methods), the latter being the main difference between objects and simple data structures.
A model in which domain classes only store their attributes and references to each other and all processes occur outside of these classes, is a anti-pattern known as Anemic Domain Model.
Therefore, it is not worth just hiding the list of disciplines within the Student class and building several other classes to manipulate the changes of this list, because you would be breaking the encapsulation.
With respect to performance, it is up to you to decide, at each moment, whether to build a Student object with all Disciplines (Eager Fetch), or only with basic Student data (Lazy Load). In fact, I cannot imagine an educational institution where the number of active students and disciplines is so great as to impact the performance of the application. If you are going to have some slowness, it will probably be for another reason, such as not optimized code, error in server configuration, undersized hardware etc.
2) If you use a data structure, such as a Treemap, relating Student lists to each Discipline object, you may see this Treemap being passed as a parameter from one side to the other in various methods. All these methods would have to implement the existing controls in the relationship between these two objects, or worse, it would take a third class, specialist in Students and Disciplines, just for that.
As one of your concerns is with security, how to ensure that all parts of the code that use this Treemap will obey all the rules?
Of what class would be the responsibility of comparing the data of this Treemap with other data, for example, another Treemap with the previous disciplines already taken by a Student?
A criterion that can also help you make this kind of design is to check whether the proposed implementation makes sense in the real world.
Does it make sense for a student to know how many subjects he should enroll in? Or to know that he cannot enroll in more than X disciplines?
It makes sense for a student to know how to answer which subjects he takes?
It makes sense for a student to ask to leave a class?
Given a subject, it makes sense for a student to know who the teacher is, case Curse that discipline?
You represent different things in both examples, in the case of the right, you have a Treemap that represents a subject, it could be an abstract type of data, a new class, but in this case, what you say is, a class has several subjects, and each subject has its students, something similar to a master’s degree, each one chooses the subjects he wants to attend, in the second case, a class has its students, something similar to high school, you have two distinct abstractions in it, first establish what you want to represent, because I was in doubt when I went to create a performance model.
– Lucas Fantacucci
No no, I think I expressed it wrong. When I say class, it’s like a subject. And each subject has its students registered. Treemap would be a data structure to store references for the different subjects registered and the list of students who are attending the same. I will edit the post
– Marcos de Andrade
Another question when we think about application performance is, are you thinking about using frameworks? if yes, which ones? or want to work with pure java?
– Lucas Fantacucci
The specific example would be in pure Java, specifically to understand (without frameworks influence) what the performance and security difference would be between owning a structure to manipulate the data, or manipulating it directly through access to class attributes. It can be assumed that there were hundreds of registered subjects, each with its student list
– Marcos de Andrade
I would use a Hashset in place of Treemap, it would get much more performatic.
– Emanoel
@Emanoel the I had thought of Hashset, but the natural ordering property of Treemap makes a lot of sense (in my conception) for a list of students. I’ll see if I can find some examples of performance comparison between the two structures, honestly both are barely cited in college and I’m studying on the outside, so I have to run after haha
– Marcos de Andrade