Best practice to collect information from child objects

Posted by Markus on Programmers See other posts from Programmers or by Markus
Published on 2014-05-27T15:31:31Z Indexed on 2014/05/27 15:56 UTC
Read the original article Hit count: 179

I'm regularly seeing the following pattern:

public abstract class BaseItem
{
    BaseItem[] children;

    // ...

    public void DoSomethingWithStuff()
    {
        StuffCollection collection = new StuffCollection();
        foreach(child c : children) c.AddRequiredStuff(collection);
        // do something with the collection ...
    }

    public abstract void AddRequiredStuff(StuffCollection collection);
}

public class ConcreteItem : BaseItem
{
    // ...

    public override void AddRequiredStuff(StuffCollection collection)
    {
        Stuff stuff;
        // ...
        collection.Add(stuff);
    }
}

Where I would use something like this, for better information hiding:

public abstract class BaseItem
{
    BaseItem[] children;

    // ...

    public void DoSomethingWithStuff()
    {
        StuffCollection collection = new StuffCollection();
        foreach(child c : children) collection.AddRange(c.RequiredStuff());
        // do something with the collection ...
    }

    public abstract StuffCollection RequiredStuff();
}

public class ConcreteItem : BaseItem
{
    // ...

    public override StuffCollection RequiredStuff()
    {
        StuffCollection stuffCollection;

        Stuff stuff;
        // ...
        stuffCollection.Add(stuff);

        return stuffCollection;
    }
}

What are pros and cons of each solution?

For me, giving the implementation access to parent's information is some how disconcerting. On the other hand, initializing a new list, just to collect the items is a useless overhead ...

What is the better design? How would it change, if DoSomethingWithStuff wouldn't be part of BaseItem but a third class?

PS: there might be missing semicolons, or typos; sorry for that! The above code is not meant to be executed, but just for illustration.

© Programmers or respective owner

Related posts about c#

Related posts about design