Hi Stackoverflow, I have a design problem I cannot find a clean and nice solution to. I am developing in PHP but I believe this could occur in any language. My basic problem is that I have two objects that have circular interdependence at some level of indirection. That means I have a class (call it F) implementing the Facade pattern which contains an object (of class B) which itself needs an object of class A to be created. class A's constructor itself needs a facade F to be created => I have circular interdependence of objects.
I believe I cannot resolve the circular interdependence (the objects basicly implement a finite state machine with a loop using the state pattern) so I am looking for a clean solution. I came up with two possible solutions myself, but I dont think either is especially elegant:
Have class A implement a setFacade(F $facace) method and remove the entire facade from the constructor and just set it after A and the facade are created. Objects of class A cannot work without the facade so this would actually create an object of class A which is not able to do anything until setFacade is called and it would reduce encapsulation and would allow to replace the facade at object runtime, which I also don't like.
Implement something like a Promise that is passed to A instead of the facade which will be able to resolve the facade later as soon it is created. I dont like to introduce this additional indirection layer, especially because I have no good place to actually resolve the promise than in the methods that handle buisness logic inside of A, which could a) produce terrible errors and (more important) b) woul need me to check if the promise was already resolved or if i will need to resolve it now, whenever the buisness logic is called. That's just terrible design in my eyes.
So if anyone can come up with a better solution or could support one of my possible solutions with a good argument I'd really happy.