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?
Additional details
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.
Edit:
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.
5
The simplest (easiest to understand, read, and maintain) is to simply define, in the parent:
public int Summary
{
get { return this.Children.Sum(x => x.SomeVariable); }
}
If you don’t have a performance problem (and it sounds like you don’t), I’d do that. If you’re worried about updating the display of a parent node when a child node changes, there are two simple alternatives:
- Use a timer and just recalculate every X seconds (assuming the recalculate time << X), or
- Use a message bus of some kind. Inject the message bus object into all nodes. When any node changes something that a parent might care about, send a message, and have that message trigger a refresh as necessary.
I tried events once, and if you’re actively adding and removing nodes, it gets messy because if you don’t properly unsubscribe from each event when a node is removed, you could get dangling references, and the equivalent of a memory leak.
1
Unless this list will contain millions of entries, just do the simplest thing: iterate and sum. Save anything more complicated until the simple code is measurably too slow.
Sounds like a textbook case for the Observer pattern. Make each node an observer of its children, and have each child inform its observers whenever its value or state changes (since I assume you’ll want to adjust the cost if a child is de-selected).