I have a hierarchical structure of objects (lets say, 3 levels).
Class A is composed with class B, class B with class C.
The initialization does something like :
$a1 = new A()
$a1->add('B1', $b11 = new B())
$a1->add('B2', $b12 = new B())
$b11->add('C1', $c111 = new C())
$b12->add('C1', $c121 = new C())
...
My problem occurs when $c121 wants to retrieve $c111. I currently know 2 ways to achieve that :
1/ Assume that A is a singleton : it gives me the ability to dash back to the root from any node of the tree. I can then address any node from the root with a full “pathname”.
2/ Give to each object its ancestor, so they can go back (by ancestor reference) and forth (by pathname) through the tree.
While both serve my needs, I can’t find any of those solutions satisfying.
Am I missing something ? This should be a common problem, yet I can’t figure out what design pattern could match this issue.
EDIT Note that the hierarchy isn’t composed of similar items. So it is a fixed hierarchy. Only the quantities of items at each level is fluctuating. (eg. Clients, Orders and Products Ordered)
If your classes are disparate, then I’d treat them as such (ie you don’t have a tree per se).
It seems like you’re trying to treat the disparate elements as if they were somehow similar.
If you need access to a containing class (what you’re calling parent), you will need to provide a reference.
In your example above, I would give your associated objects a reference to object “holding” it, and give the C class a means to get its siblings.
Another way to look at that is: