I have an object hierarchy with a number of leaf nodes that will contribute to summary values for the parent object (specifically: project cost and square footage).
What's the most efficient way to tally up those values while the project is being designed?
Parent ListOne NodeA NodeA.1 NodeA.2 ... NodeB NodeB.1 NodeB.2 ... ListTwo ...
- There will only be one dot-# leaf node selected at a time, so it will be either NodeA.1 OR NodeA.2 but not both being selected.
- Whether or not NodeA and NodeB are selected are independent of each other. So I could have any combination of selected / active Nodes A -> Z
One approach is simply to iterate through each List, each Node, and each Leaf Node and only sum the values when the leaf node is selected.
Another approach would be to create an event handler for the parent project variables that each child can then fire against when their values change. The wrinkle here is that I'll have Node specific values that I'll want to track as well. In other words, I'm tracking Project Cost, List Cost and Node Cost.
Another approach would be a hierarchy of event handlers that fire as the selected item changes and then notify their parent of the change. So the change in a leaf node would eventually cascade back up to the Project after a number of events having been fired.
Right now, the overall project structure is pretty small - only three or four Lists, each with about six Nodes that have four to six Leaf Nodes each. Because of the smaller size, I'm not worried as much about computation time as I am about maintaining this and being able to extend it in the future as more Lists, Nodes, etc... are added.
The project driving this question is written in C# and actually has one more layer between the List and the final leaf nodes. I think the question is relevant to C++ and Java programmers since it's dealing with the responsibilities between children and parent objects when a value of the parent depends upon its children and grand-children. An answer tailored to C# would be gravy, but I'm looking for the most robust / maintainable approach in this case.