Need advice on OOP.

There is a main class in which a lot of code has accumulated, I want to unload it, that is, if possible, break it into separate classes.

Suppose I have a logical piece, which I carry into a separate class, but quite often the members of the main class are used in it. And there is the problem of choosing the method of access to these members. For the decision I consider such options:

  1. in add. class, create used variables, and initialize some of them in the constructor, transfer some of them to the required method with arguments

(what problem I see) repeated code, large list of arguments (constructor / method)

  1. in the constructor add. class to declare the type parameter of the parent class, and when creating the add object. pass the class to the this constructor ( that is, we will receive a link to the parent ) and use the parent link to use the required fields

(what problem I see) As far as I know, it is considered a bad practice to transfer the reference to the parent to the objects. The instance does not need to know what is happening up the hierarchy, the manipulations go only down the hierarchy. Right?

Not an option to leave this piece of code in place.

It will work this way and that, but how to do better? I need the best practices.

    2 answers 2

    Probably the very architecture of the separation is not quite well understood by you. And in fact, your classes are still too "big" (this is essentially still another class, albeit divided into two files).

    If the question arises as to how they should share joint functions, then their separation is not completed and a third one, and probably a fourth, etc., is also needed. ... Each class, like the method, must perform only one function. If it is a holder, then it is only responsible for storing and adding / deleting data. If it is a filter, then it only filters. If it is a converter, then it only converts.

    And, if there is a mixture of functions, then you are trying to grow a "dragon", even if it is many-headed, but this beast will devour your time and energy.

    Select more classes. They, as well as methods, should be rather short and autonomous. Imagine that you completely stuff this class into another application, can it work there without alterations?

    One function = one class, one class = one function!

    • one
      By the way, I came to the same conclusion that I did not separate the entities sufficiently. Self-sufficiency, this is what you describe for each object, this is what you should try to create. Fully agree with your answer. Another question, this problem is not a problem at all? After all, you can leave it so, this is all because I want the best code. But then it turns out that in fact, in such God classes, the op is promoted and a procedure appears. - boneferz
    • Yes, quite right - VBugaenko

    In this case, it would be nice to know the problem itself, and not its solutions. I think it would be possible to make the parent class use the children. Or even break the logic into several classes. As an option, your Parent class and Child can become classes representing any logic, and the fields and data that are used by the Parent and Child classes can be divided into a separate class. But again, it all depends on the task.