samspade
Member
I'm trying to create a custom event order for a group of objects that runs each step as each object down the chain needs its predecessor to run their event before it runs its own. More specifically there is a main object which runs calculations, then there can be n number of sub objects which run calculations off of that which in turn can have n number of sub objects which run calculations off of their immediate parent and so on.
So
main object runs its calculations
The catch is that I was trying to accomplish this without the parent nodes knowing about their children. If the parents know about their children then I can just have each parent run its code then run the code for all its children.
One solution is to have each node track whether it has run its code that step and then loop through all the nodes and if it has not run its code, traverse up the tree until it finds either the source or a node which has run its code and then traverse backwards running the code at every step.
Code isn't necessary but I'm wondering if there is a basic design pattern or method of doing this that I am missing.
So
main object runs its calculations
first layer of sub objects run calculations on main
Essentially it is a tree structure where nodes farther down the branches require calculations farther up to be completed first and there can be any number of nodes at any depth.second layer of sub object run calculations on first layer
.....repeat as far as necessary
The catch is that I was trying to accomplish this without the parent nodes knowing about their children. If the parents know about their children then I can just have each parent run its code then run the code for all its children.
One solution is to have each node track whether it has run its code that step and then loop through all the nodes and if it has not run its code, traverse up the tree until it finds either the source or a node which has run its code and then traverse backwards running the code at every step.
The other alternative I see seems like it would be to maintain a separate structure (likely map or list) that tracks the tree structure. So the parents don't know about their children, but the structure does and can then do the simple thing of traversing down the tree. Code isn't necessary but I'm wondering if there is a basic design pattern or method of doing this that I am missing.