tags:

views:

28

answers:

1

I'm trying to figure out how can i merge iterator and composite objects. what can be the best implementation for a struct below.

class Composite<Item> : Item
{

}

class Iterator<Item>
{

}

class Iterable<Item>
{

}
A: 

In C#, it's rare that you would do iterators in this method. Typically, you'd just use your item within a generic collection.

As for the Composite Pattern - the main motivation behind this pattern is to allow your composite to be treated as if it's just an item. This typically means you can "combine" a composite with iteration merely by doing:

// Given:
public class Composite<Item> : IItem {}

You can do:

List<IItem> items = new List<IItem>();
items.Add(new Item());
items.Add(new Composite<Item>());
items.Add(new Item());

// Iterate through the items...
foreach(var item in items)
{
    item.Foo();
}
Reed Copsey