Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4113610
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:20:51+00:00 2026-05-20T22:20:51+00:00

I am using a composite pattern as shown in the class diagram below. Basically

  • 0

I am using a composite pattern as shown in the class diagram below. Basically my primary leaf (an Allocation) has a single property, Duration, that I want to accumulate in various composites.

I also want to have a count of allocations, and the ability to return the allocations as a collection I can use for data binding in presentations.

This question is about the C# implementation, specifically:

  1. There is an interesting post here that has an extension to do this recursively, as well as an alternative approach that avoids recursion altogether. Would you have a common method to get at the Allocations the way I am doing? I haven’t decided yet if the code I wrote feels awkward because I am not comfortable with pattern, or if it is awkward!
  2. Do you see any easy opportunities here to cache the results of the first iteration and thereby minimize further iteration?

Cheers,
Berryl

this class diagram

Code in the Composite class

public class AllocationComposite : AllocationNode
{
    protected readonly IList<AllocationNode> _children;
    public AllocationComposite() { _children = new List<AllocationNode>(); }

    #region Implementation of AllocationNode

    public override void Adopt(AllocationNode node) ...
    public override void Adopt(IEnumerable<AllocationNode> nodes)...
    public override void Orphan(AllocationNode node)...
    public override void Orphan(IEnumerable<AllocationNode> nodes)...

    public override IEnumerable<AllocationNode> Descendants
    {
        get
        {
            return _children.Concat(_children.SelectMany(child => child.Descendants));
        }
    }

    public override IEnumerable<Allocation> Allocations
    {
        get
        {
            return Descendants.OfType<Allocation>();
        }
    }

    public override TimeSpan Duration {
        get {
            return Allocations.Aggregate(TimeSpan.Zero, (current, child) => current + child.Duration);
         }
    }

    public override int Count { get { return Allocations.Count(); } }

    #endregion

    public override string ToString() {
        return String.Format("{0} for {1}", 
            "allocation".PluralizeWithCount(Count), 
            "hour".PluralizeWithCount(Duration.TotalHours, "F2"));
    }
}

}

Default implementation in abstract Component

public abstract class AllocationNode: Entity, IAllocationNode
{
    #region Implementation of AllocationNode

    public virtual TimeSpan Duration { get { return TimeSpan.Zero; } set {  } }

    public virtual void Adopt(AllocationNode node) { throw new InvalidOperationException(...)); }
    public virtual void Adopt(IEnumerable<AllocationNode> nodes) { throw new InvalidOperationException(...)); }
    public virtual void Orphan(AllocationNode node) { throw new InvalidOperationException(...)); }
    public virtual void Orphan(IEnumerable<AllocationNode> nodes) { throw new InvalidOperationException(...)); }

    public virtual int Count { get { return 1; } }

    public virtual IEnumerable<AllocationNode> Descendants { get { return Enumerable.Empty<AllocationNode>(); } }

    public virtual IEnumerable<Allocation> Allocations { get { return Enumerable.Empty<Allocation>(); } }

    #endregion
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:20:52+00:00Added an answer on May 20, 2026 at 10:20 pm
    1. Do you want to have a collection of all nodes flattened (IEnumerable allNodes)? If so, after a quick look at the post you linked, I think that both solutions are OK. If the cosiderations LukeH mentions don’t apply to your case, I would stick to LINQ solution because of its clarity, but it’s up to you.

    2. Caching is easy if you don’t modify your structure. It gets very complicated if addition and deletions take place.
      Anyway, what do you want to achieve with caching? Have the descendants count cached? Descendants themselves? On which level? Root level or any level?

    If you have an abstract class, it would be more elegant to make its methods abstract rather than virtual throwing exceptions or returning empty collections.

    PS. The class diagram is not synchronized with the code (i.e. placement of Duration property)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is a first attempt at using a Composite pattern. It works in the
I have implemented a simple Composite pattern using SplObjectStorage, like the example above: class
I'm using the Composite pattern to represent a hierarchical data structure: CompositeNodeA Leaf1 Leaf2
Consider the code below (which has been simplified). I have a service class that
I'm using the composite pattern to have reusable elements to build a page. for
I'm using the Composite Application Library with Silverlight, and I need to add three
I'm using NUnit v2.5 to compare strings that contain composite Unicode characters. Although comparison
I'm using the following org.eclipse.jface.viewers.CheckboxCellEditor.CheckboxCellEditor(Composite parent) I'm creating a table viewer with cellEditors and
Setup : Using PowerBuilder 6.5. I have a composite report (with a report header)
Why is the rejection of composite keys in favor of all tables using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.