Suppose we have a design where objects have a possibly reciprocating dependency on other objects:
struct Object
{
...
virtual void method();
private:
std::vector<std::shared_ptr<Object>> siblings;
};
There is danger of circular dependencies. This could be broken by use of weak pointers, but then the design immediately becomes fragile as it introduces the very likely possibility of sibling dependencies expiring while still in use.
How do we fix this problem without introducing circular dependencies? Is there a design pattern for this and/or are there garbage collection libraries that can be used to elegantly solve this problem?
Of course, I respect that the ideal solution is to avoid designs where mutual dependencies occur, but for the purposes of the current question, please work with the constraint that a mutual dependency design cannot be avoided. By way of motivating example, consider a recurrent neural network where each neuron is represented as an object that explicitly stores references to its connected neurons.
I've tagged the question C++
, but language-agnostic answers are also welcomed.
Aucun commentaire:
Enregistrer un commentaire