473,288 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

C# v2.0 Iterators and the Visitor design pattern

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[i].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.

Nov 17 '05 #1
2 2329
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" <ja**********@BiteMeHotmail.com> wrote in message
news:65********************@comcast.com...
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[i].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.

Nov 17 '05 #2
Thanks! Extremely useful...
see also Jerome Laban's Reflective Visitor
http://msdn.labtech.epitech.net/Blog...005/04/02.aspx

--
Ahmed Qurashi
www.okaq.com
aq@okaq.com

"Fred Mellender" <no****************@frontiernet.net> wrote in message
news:zq*************@news01.roc.ny...
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" <ja**********@BiteMeHotmail.com> wrote in message
news:65********************@comcast.com...
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[i].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.


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: richard.forrest1 | last post by:
I have a problem with an abstract interface class whose implementation classes need to return different iterator types (but with the same value_types etc). Classes A and B both conform to the...
12
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of...
17
by: Merlin | last post by:
Probably there is no right or wrong answer to this but I thought to ask to put my mind at rest. Ok lets say you have a object hierarchy (eg. the Glyph in Lexi from GOF book) and you want to use the...
0
by: Siphiuel | last post by:
Hi everyone. When using visitor pattern, we have a nasty dependence on the types of visitable objects that is coded way on top on the visitor hierarchy. i mean, like this: class AbstractVisitor...
1
by: RedLars | last post by:
Hi, Given this class definition, public class Node { Node parent; object current; ArrayList children;
1
by: JosAH | last post by:
Greetings, this week we let go of all that algebraic stuff and concentrate a bit more on what object oriented programming is all about. Java claims to support OO, so why not use it? In this...
3
by: n.torrey.pines | last post by:
Hi I work with a nested tree-like data structure template (that happens to be a tuple of vectors of tuples of trees of something, with different tuple elements having different types) I need...
0
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns: Visitor Introduction Polymorphism requires a class hierarchy where the interface to the hierarchy is in the base class. Virtual functions allow derived classes to override base...
3
by: aaragon | last post by:
Hello everyone, I've been trying to work with the visitor design pattern, and it works fine except for the following. Let's suppose that we have a fixed hierarchy of classes (many of them)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.