Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
1  
Are you looking for a C# solution? If so, please include that info in the question, or at least the tags. –  kevin cline Aug 2 '12 at 16:57
    
@kevincline - Yes, I happen to be writing in C#. I thought the question was somewhat language agnostic or would be relevant for C++ and Java as well. So the answer doesn't have to be C# specific. –  GlenH7 Aug 2 '12 at 17:01
    
The beginning of your question talks about efficiency, but in your edit you say "the most robust / maintainable approach". Efficiency to me means execution time/resources, and that's different than optimizing for robust/maintainable. –  Scott Whitlock Aug 2 '12 at 17:37
    
@ScottWhitlock - I'm referring to lower case e efficiency, not big E Efficiency. So programmatic efficiency is the goal, not necessarily big O notation type Efficiency. Apologies for the confusion with the use of the term. –  GlenH7 Aug 2 '12 at 17:45
    
@GlenH7 - fair enough, I just found it confusing. –  Scott Whitlock Aug 2 '12 at 18:00

3 Answers 3

up vote 6 down vote accepted

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.

share|improve this answer
1  
+1: Definitely the best solution for C#. If only Java programming were as easy :-( –  kevin cline Aug 2 '12 at 22:16

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).

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.