C# v2.0 Iterators and the Visitor design pattern

W

Wavemaker

I've been playing with C# v2.0 today, and having quite a bit of fun. The
new version has added iterators. The iterators are coded directly into
the class to be iterated. For example:

public IEnumerable<int> Visit(SomeVisitor visitor)
{
int position = 0;

for(int i = 0; i < Count; i++)
{
yield return position;

data.Accept(visitor);

position++;
}
}

This can then be used in a foreach construct:

foreach(int position in someObject.Visit(this))
{
// Do something with position here.
}

Where 'someObject' is the object to traverse, the class of which
contains the iterator code above, and 'this' is an instance of a class
derived from 'SomeVisitor.'

So as the object is traversed, the iterator first returns the position
information. Here it is just a simple integer, but it could be anything,
something much more complex, perhaps. Then the Accept method is called.

The idea is that you can traverse a collection where the Visitor design
pattern is used without having to explicitely call the Accept method;
the iterator takes care of that for you. Plus, you get additional
information about the traversal that can be used as well. Care would
have to be taken so that you know which comes first, the value returned
from the iterator or the Accept method being called. But it seems worth
the effort.

This can be coded in different ways in other languages to the same
effect, but I thought this use of C# v2.0 iterators together with the
Visitor pattern was kinda neat.
 
A

Ahmed Qurashi

Thanks! Extremely useful...
see also Jerome Laban's Reflective Visitor
http://msdn.labtech.epitech.net/Blogs/jaylee/archive/2005/04/02.aspx

--
Ahmed Qurashi
www.okaq.com
(e-mail address removed)

Fred Mellender said:
You might also be interested in using iterators to explore graphs in various
traversal patterns (depth first, breadth first, etc). I have coded examples
in http://www.frontiernet.net/~fredm/dps/Contents.htm




Wavemaker said:
I've been playing with C# v2.0 today, and having quite a bit of fun. The
new version has added iterators. The iterators are coded directly into
the class to be iterated. For example:

public IEnumerable<int> Visit(SomeVisitor visitor)
{
int position = 0;

for(int i = 0; i < Count; i++)
{
yield return position;

data.Accept(visitor);

position++;
}
}

This can then be used in a foreach construct:

foreach(int position in someObject.Visit(this))
{
// Do something with position here.
}

Where 'someObject' is the object to traverse, the class of which
contains the iterator code above, and 'this' is an instance of a class
derived from 'SomeVisitor.'

So as the object is traversed, the iterator first returns the position
information. Here it is just a simple integer, but it could be anything,
something much more complex, perhaps. Then the Accept method is called.

The idea is that you can traverse a collection where the Visitor design
pattern is used without having to explicitely call the Accept method;
the iterator takes care of that for you. Plus, you get additional
information about the traversal that can be used as well. Care would
have to be taken so that you know which comes first, the value returned
from the iterator or the Accept method being called. But it seems worth
the effort.

This can be coded in different ways in other languages to the same
effect, but I thought this use of C# v2.0 iterators together with the
Visitor pattern was kinda neat.

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top