Efficient Summary calculation method or approach?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật