Posted By: ElucidWeb | Jul 29th, 2006 @ 5:58 PM
page 1 of 1
Comments: 4 | Views: 4861
So something I really hate doing is iterating over a composite, especially complex ones.  So I have a program that builds a file system Component using the Composite design pattern.  It doesnt take to long to build the entire thing and I have used the Iterator deisgn pattern to build an Iterator class around it. No problem if not annoying to wrap your head around how some if it branches out.

My question is what experience have you had doing this and any gotchas along the way?  I re-wrote this based on a recursive design I had before that was blowing the stack every now and then, of course thats not good and thats whay Iterator design is the best.

Just curious, thanks!
littleguru
littleguru
allein, allein,... allein, allein!
Yah. Writing an iterator that goes through a composite is just a little challenge. There are also different ways how to iterate through it: For example in a tree, you could do first the current level, then step down or first step down left and then right and then the current level etc...

The first approach I would do is also the recursive way. If that doesn't work I would look where to inline the recursive part and replace it with a loop.

Best is to extract the iterator stuff into an own class.
eddwo
eddwo
Wheres my head at?
Isn't it much easier now with C# 2.0 Iterators?
You don't have to keep track of the state manually, just write a loop and yield return one value at a time.
littleguru
littleguru
allein, allein,... allein, allein!
eddwo wrote:
Isn't it much easier now with C# 2.0 Iterators?
You don't have to keep track of the state manually, just write a loop and yield return one value at a time.


Should be kind of easy with yield Smiley The problem of stack overflow remains... But that is avoided with inlining methods and recursive calls.
page 1 of 1
Comments: 4 | Views: 4861