Hello, I ask you to help me develop the logic of classes. The fact is that I need to build the correct hierarchy of products (the composition of one product). Input data looks like this:

  • 1 Machine
    • 1.1 Engine
      • 1.1.1 Piston
      • 1.1.2 Sleeve
    • 1.2 Carburetor
      • 1.2.1 Bolt
        • 1.2.1.1 Screw
    • 1.3 Steering wheel

Accordingly, what the product consists of is unknown to me in advance (classes must somehow be created dynamically).

It is understood that the classes of products will be with the same fields, that is, it seems to me, the hierarchy of the same classes.

Tell me, please, a concise implementation on .Net 4.5 or where to dig for the development of such a hierarchy.

With regards to the required fields, this is exactly "Worker, creating a part" and "his work time"

  • Same classes are the same class. That is, you can, of course, declare several classes with an identical structure - but from the point of view of the compiler, it will still be the same class (duck typing, that's all). You mean product categories maybe? - eastwing
  • the products are the same everywhere (I think the class is the same), but I somehow need to realize the dependence from the parent class to the children, that is, the same class everywhere, but with a hierarchy. - astral
  • one
    Here you have not hierarchy / inheritance (is), but composition / aggregation (has). - Alexander Petrov
  • I strongly advise you to briefly escape from the solution of specific problems and read some books on the PLO (at least Gradi Bucha, or something new can be good), and then return to the actual programming. - m. vokhm

1 answer 1

In your question there is a misunderstanding of the term class itself . I'm afraid you should start by learning the basics of OOP (read the article for a start), then start learning C # .


Now let's try to deal with your question. What did you mean? If you still want to build a class hierarchy, then you want to do it wrong. It is obvious that if the class "Sleeve" is a refinement of the class "Car", then this, to put it mildly, is incorrect. That is, the program, of course, doesn’t matter, but from a human point of view it simply does not make sense. How is the coasse hierarchy? Just like in life! For example, you have the class "Car", the derived class from which is "Moskvich". The Muscovite inherits all his attributes from the Car, but he has his own unique ones (well, I don’t know, the method "Navon in the cabin with gasoline AI-76"). From Moskvich, you can make another class - "Moskvich-412", which is also a Moskvich (and, accordingly, a car), but has a number of additions, for example, the most modern design and powerful roar.

Thus, the class hierarchy is a refinement tree. The concept of "Sleeve", as you, I hope, understand, can not clarify the concept of "car". Read more about classes read the links that I cited above.


But perhaps I misunderstood you, and you need some kind of production journal that you see organized as a tree? For example, are there specific bushings and pistons that have production time, which in turn affects the engine production time, which affects the production time of the car?

There are various options for implementation. For example, you might have a product class:

public class Product { public float ProductionTime; public string Worker; } 

Based on the product, you can describe every detail, as well as a car. In this case, note that all these classes will not inherit from the car (for example), they will all be derived from the product.

The class "Engine" will contain the fields "Bush" and "Piston", and have their own method of calculating the production time.

The class "Car" - contain the fields "Engine", "Carburetor" and "Steering Wheel", etc.

I don’t cite the code, since, frankly, I don’t see much sense in this. Do not be offended, but in my opinion, it’s too early for you to solve this problem - see the beginning of the answer.

  • Thank you very much for the answer, I simply do not know the practical implementation of such a task. That, apparently, mistakenly asked about the class hierarchy. But I still know the classes. It all comes down to building hierarchical links from the product to its components. How this is implemented in practice - I have not found examples. For what is the hierarchy of the composition of the product I do not know in advance. - astral
  • one
    @astral - the Linker pattern will definitely suit you. - Alexander Petrov