I have a system where Object A can at will produce Object B. One Object A can produce any number of Object B, and there are multiple Object A‘s.
It is useful in my program to know WHICH Object A produced which Object B (for various reasons). Currently, whenever an Object B is created, I just store the Object A that created it as a parameter inside Object B. But this doesn’t feel right to me.
Question 1: Is there a way to store simply a reference to the correct Object A inside the Object B, instead of the whole object? Or is that actually what’s happening anyways?
Question 2: Is doing it the way I am now inefficient for memory?
Question 1:
Yes it is possible. You can pass a reference to creator object e.g. by constructor. It could be also by public property setter or public field but I would recommend to use constructor.
Question 2:
Object reference takes
4bytes/8byteson32bit/64bit OS. So you can roughly imagine the additional memory consumption and determine, if it is an issue for you (IMHO it would not be an issue for most of the applications).