(Just in case that there were no misunderstandings: this is not about inheritance, but about the creation of objects)

With the usual hierarchy of parent-child classes, the parent knows everything about the child and can fully interact with it. But what to do when children need to know about each other and interact with each other?

How to implement it correctly?

I implement it as follows - for this I use a separate class, say GlobalData , I pass it a reference to the used object, let it be objA . And in another objB object, objB get access to the desired method of the first object.

 GlobalData.objA.needMethod(); 

At the same time I have GlobalData - singleton .


What is the right thing to do in this case? What templates are used for this? Or do I still do everything right?

Here is an example illustration. enter image description here

  • one
    Something on the description is not entirely clear why GlobalData is needed. Maybe some visual, but minimal, sample code? Perhaps instead of GlobalData, you can simply transfer to the dependent object on which it depends upon creation. - Grundy
  • @Grundy GlobalData - for a common access point. Instead of making the fields and methods of the object to be used static. - boneferz
  • And why was it necessary to make fields and methods static? - Grundy
  • @Grundy Why? You have two parents, A and B. How does a child of parent A know about the existence of a child of parent B? Only through a static link, but how else? But here, static, we can not even use, because the child objects will not be in a single copy. - boneferz
  • And why would he even know about the child of parent B? - Grundy

0