We write an application for acceptance of various nomenclature (equipment) from suppliers (monitors, printers, etc. office equipment). Acceptance is carried out on the basis of a document in which there is an N-th number of lines. Each line is a separate nomenclature with the name, code, price, number, and so on. For each item of the nomenclature is assigned one inventory number. Now to describe this, we use the following classes:

// Π­Ρ‚ΠΎΡ‚ класс описываСт Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ class Doc { String docNum; // НомСр Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Π° List<DocRow> rows; // Π‘Ρ‚Ρ€ΠΎΠΊΠΈ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Π° } // Π­Ρ‚ΠΎΡ‚ класс описываСт строку Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Π° class DocRow { Nomen nomen; // НомСнклатура int count; // Кол-Π²ΠΎ ΠΏΠΎ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Ρƒ int price; // Π¦Π΅Π½Π° ΠΏΠΎ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Ρƒ List<Invent> invents; // Бписок ΠΈΠ½Π²Π΅Π½Ρ‚Π°Ρ€Π½Ρ‹Ρ… Π½ΠΎΠΌΠ΅Ρ€ΠΎΠ² } // Π­Ρ‚ΠΎΡ‚ класс описываСт Π½ΠΎΠΌΠ΅Π½ΠΊΠ»Π°Ρ‚ΡƒΡ€Ρƒ class Nomen { String code; // Код String name; // НаимСнованиС } // Π­Ρ‚ΠΎΡ‚ класс описываСт ΠΈΠ½Π²Π΅Π½Ρ‚Π°Ρ€Π½Ρ‹ΠΉ Π½ΠΎΠΌΠ΅Ρ€ class Invent { Nomen nomen; // НомСнклатура String barcode; // Π“Π΅Π½Π΅Ρ€ΠΈΡ€ΡƒΠ΅ΠΌΡ‹ΠΉ ΡˆΡ‚Ρ€ΠΈΡ…-ΠΊΠΎΠ΄ } 

As a result, the nesting of classes is as follows:

enter image description here

It can be seen that the class Nomen is on several levels and, as it were, is dragged below. The question is, how much is right to do that? What are the basic rules?

We did this because copies of links to an instance of the Nomen class at different levels are very convenient. For example, we have a method for printing a packet of inventory numbers print(List<Invent> invents) on a printer. When printing the inventory number, Invent.barcode needs to display the same name. Thanks to access to Invent.Nomen.name, we can do it without any problems, without adding additional logic to find this name.

The Invent class, as it were, can communicate its bar code and type of nomenclature, which makes it sufficiently self-sufficient. Moreover, in the future, we can use the Invent class in general outside the DocRow class , and then we definitely need to understand to which type of item the inventory number belongs to.

What will prompt colleagues with years of experience?

Thank.

    1 answer 1

    I do not have many years of experience, but why do not you want to lower the print method to the level of the Invent class? And use this:

     class DocRow { ... void print() { foreach(Invent invent : invents) { invent.print(); } } ... } 
    • Even if you call the print () method of the Invent object, it still must contain a Nomen object to print all the necessary information, so where it will be now is not so important. The essence of the question is how to correctly produce links to the object Nomen. - UltimateOrb
    • @UltimateOrb A good programming practice is low system connectivity. The more classes will be tied to Nomen or some other, the greater will be the "connectivity" of the system, and the more difficult it will be to modify it. In my example, the Nomen field is no longer needed in the class in the DocRow class, so the Nomen class will only be associated with the Invent class. - Alexander Potashev
    • This is all good. But at some point in time, we have a task that simply displays the document lines in the form: name of the item -> number -> price. And here it would be convenient to just run DocRow to take the count, price, nomen.name. - UltimateOrb
    • Seems understood your complexity. I’ll clarify: (1) Can count in DocRow class differ from the count in List <Invent>? (2) The price in DocRow is the total price of all Inventors in this sheet? (3) If "2" is yes, then is it possible to add a price field to the invent inventor and is it the same for all inventors with the same item number? - Alexander Potashev
    • To push print in DocRow is definitely bad. After all, DocRow is just data, and here, printing logic from a flounder bay. And if we want to print a little bit differently for each case? And if we want to generally output as JSON in api and send over the network? Plus it increases connectivity many times more than just duplicated Nomen. Yes, and duplicate Nomen ugly. IMHO print (List <invent> invents, Nomen nomen) looks better. - Uraty