Better structuring of an object-oriented code

2

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

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

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

The big difference between the two is that, in the second, the ArrayList is an attribute of the class, while in the first a structure was created (I thought of TreeMap, or is there a more appropriate structure?) to store access to the data.

    
asked by anonymous 19.03.2018 / 13:28

2 answers

1

First, just as a suggestion to make it easier to understand your model, I suggest you call your "Discipline" class. Calling "Class" is confusing because all of them are classes (in terms of OO) and "Matter" are actually the subjects covered in a discipline.

Evaluating the scenarios you've proposed:

1) If you use a list of Disciplines as an attribute of the Student class, you delegate to the Student class any changes to your Disciplines. In this case, you would be applying the Encapsulation concept, meaning that the responsibility for knowing the implementation details would be hidden in the Student class.

Please answer the following questions to see if the above approach suits your use case:

  • What are the business rules applicable to the use case? For example, is there a minimum / maximum number of students per subject, or a minimum / maximum number of subjects per student?

  • There are disciplines that have others as predecessors, and it is necessary to consult if that Student has already taken all the predecessors before enrolling him?

In these examples, Student methods would be responsible for this control, regardless of whether another class is changing the disciplines for that student.

So 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 an anti-pattern known as Anemic Domain Model .

So it's no use just hiding the list of disciplines within the Student class and building several other classes to handle the changes to this list, since you would be breaking the encapsulation.

With respect to performance, it is up to you to decide, at any given moment, whether to construct a Student object with all the Disciplines ( Eager Fetch ), or only with the basic data of the Student > Lazy Load ). In fact, I can not 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 slow, it will probably be for another reason, such as unoptimized code, server configuration error, undersized hardware etc.

2) If you use a data structure, such as a TreeMap, relating lists of Students to each Discipline object, you may see that TreeMap being passed as a parameter from side to side in several methods. All of these methods would have to implement the existing controls in the relationship between these two objects, or worse, it would take a third class, Student and Discipline Specialist, just for that.

As one of your concerns is with security, how do you ensure that all parts of the code that use this TreeMap will obey all the rules?

What class would be the responsibility of comparing the data of this TreeMap with other data, for example another TreeMap with the predecessor disciplines already studied by a Student?

One criterion that can also help you make this kind of design decision is to see if the proposed implementation makes sense in the real world .

  • It makes sense for a student to know how many subjects he or she should enroll in? Or know that you can not enroll in more than X disciplines?

  • Does it make sense for a student to know how to answer which subjects he / she studies?

  • Does it make sense for a student to ask to leave a discipline?

19.03.2018 / 19:46
1

The difference from these data structures:

List

  • Insertion: When entering an unordered list, the new list item will be inserted after the last object in the list
  • Removal: When removing from an unordered list, to remove an item, you must scroll through the list until you find the item you want to remove; if the list contains 100 items, you will scroll through the 100 items if the item you want is the last.
  • Update: When updating in 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 scroll through the list until you find the item

Tree

  • Insertion: When inserting into a tree, the tree can perform its balancing, which can be costly.
  • Removal: When removing 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 case above.

Regarding security, I do not understand how the chosen data structure could affect such an item.

Regarding 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 taken advantage of by your application. If you only add elements in this structure, without queries, go from list, it will be more robust for your application. If you perform a lot of updates, and searches, with few insertions and removals, go from tree.

Object Orientation

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

    
19.03.2018 / 14:26