473,320 Members | 1,977 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,320 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 2332
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...
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: 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)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.