mercredi 17 juin 2020

The properly OOP approach in operation of lists?

I have an OOP question, for example, there are lists (arrays).

Needed a mechanism that will add (save) and delete the selected values ​​from each array. The output should be for each array of its selected elements.

The logic of adding can be different for arrays. As an example, for the arr1 array, before adding to the list of selected ones, a certain condition is fulfilled, for others not.

Do I think correctly?

Arrays arr1, arr2, arr3 - data model. They have any type of data inside. The executor class must be responsible for operations on the array, it also allows you to add an item to the list of selected (favorites) and delete it from there.

Question 1 - where to store the list of selected elements for each of the array?

Question 2 - If logic with the class ArrayHandler is correct, I encounter a problem when the logic of the add method diverse for different arrays.

In this case I dont know how to check, is it that array, that need additional checking before adding to selected list.

class ArrayHandler {
    add (element) {
           // How to distinguish which input array is here to do the check before adding?
     if (element is from arr1) {
        return;
     }

      if (element is from arr2) {
          // add 
      }

    }
}

Aucun commentaire:

Enregistrer un commentaire