Better structuring of object-oriented code

Asked

Viewed 204 times

3

I came across a problem with code structuring and I needed some help. I made a diagram below with two different methods of structuring the same code (I couldn’t create the code yet) and wanted to know which is better (or if there is an even better implementation):

  • In performance/efficiency (Search, Removal, Addition, Update);
  • Safely;

The idea is simple, I define a superclass Person with two Subclasses Teacher and Student, and I have a Class (Subject, in which each subject can have its different list of students since not all students in a class are necessarily registered in the same subjects) that has a reference to the teacher who teaches the same.

The big difference between the two is that, in the second, Arraylist is an attribute of the class, while in the first one a structure was created (thought of Treemap, or is there a more suitable structure?) to store the data. The idea of Treemap is to store references to the subjects and the respective student listings.diagrama

  • 2

    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.

  • 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

  • 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?

  • 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

  • 1

    I would use a Hashset in place of Treemap, it would get much more performatic.

  • @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

Show 1 more comment

2 answers

2


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?

  • Very interesting your answer, clarified a lot of things. I thought it was important to study the business rules, I hadn’t exactly thought about some points you mentioned. I have to be more attentive. In this case, what seems to make sense to me is a double reference, the Student has a list of subjects he attends, and the discipline has a list of students (to facilitate the consultations, since the data would hardly be changed)

  • Using the information you cited and the fact that the Student would have a list of Disciplines you study, and the discipline would have a list of students, a good implementation would be the student have methods to manipulate your list of disciplines you are taking (which is an attribute of the Student class), and the Discipline class provide the construction of the list with the keys Discipline/Enrolled Students and take care of their handling, including the Treemap Students/Previously Studied Subjects, as Static methods? It would not be a better application of Separation of Concerns?

  • In fact I had not thought specifically in the case of the predecessor, it really makes sense as an Arraylist<Disciplines> of class attribute, the idea I had imagined was to make Treemap with the listing of disciplines and students registered in them as Static, because it makes no sense to instantiate a discipline to verify this, and perform something similar with the Treemap of Students/Previously Studied Subjects By that student. But now that I stopped to think, it makes more sense as an attribute Arraylist<Disciplines> in the student’s own class to encapsulate information/security

  • 1

    Got it, it’s exactly what I was imagining after the suggestions. Thanks for the strength! D

1

The difference with these data structures:

List

  • Insertion: On insertion into an unordered list, the new list item will be inserted after the last list object
  • Removal: In the removal in an unordered list, to remove an item, you need to scroll through the list until you find the item you want to remove, if the list has 100 items, you will scroll through the 100 items if the desired item is the last.
  • Update: When updating an unordered list, to remove an item, you need to scroll through the list until you find the item you want to update
  • Search: The search will also go through the entire list until you find the item

Tree

  • Insertion: When inserted into a tree, the tree can perform its balancing, which can be costly.
  • Removal: on removal in a tree, the tree can also perform its balancing.
  • Update: To find an item in a tree is more performative, because if the tree has 05 levels, it will be at most 05 interactions to find your object.
  • Search: The search in a tree is performative, also in the above case.

Regarding security, I don’t understand how the chosen data structure could affect such an item.

In relation to performance, in small datasets the difference in performance is irrelevant. It will depend a lot on your use, you must understand which use will be best used by your application.
If you just add elements in this structure, no queries, go to list, it will be more robust for your application. If you perform many updates, and searches, with few insertions and removals, go tree.

Object Orientation

Following the principle of Object Orientation, the ideal is to build a Matter class, because this way you get a greater cohesion of your code.

  • The difference between Tree and List is clear, but in the specific case this was not the issue. The question is: To have an Arraylist<Students' coupled in each Class Matter (therefore, as an attribute of the Class Matter) or not to have this information within the Class Matter, but to store it as a key/value pair in an external structure. Which of the two makes the application more performative and robust, if most of the data drive will be Queries/Updates, and not Data Removal/Addition? Consulting a structure (therefore, having to search for where the data are in the structure) is more costly?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.