#region Aggregate Item
class AggregateItem
{
#region Properties
/// <summary>
/// The AggregateItem's data
/// </summary>
public string Data { get; set; }
#endregion
#region Ctor
/// <summary>
/// Construct a new AggregateItem with
/// the given data
/// </summary>
/// <param name="data">The given data</param>
public AggregateItem(string data)
{
Data = data;
}
#endregion
}
#endregion
#region Aggregate Object
interface Aggregate
{
Iterator GetIterator();
}
class AggregateImpl : Aggregate
{
#region Members
private readonly List<AggregateItem> _aggregate;
#endregion
#region Properties
/// <summary>
/// The number of items in the
/// aggregate
/// </summary>
public int Count
{
get
{
return _aggregate.Count;
}
}
/// <summary>
/// The indexer for the aggregate
/// </summary>
public AggregateItem this[int index]
{
get
{
return _aggregate[index];
}
set
{
_aggregate[index] = value;
}
}
#endregion
#region Ctor
/// <summary>
/// Construct a new AggregateImpl
/// </summary>
public AggregateImpl()
{
_aggregate = new List<AggregateItem>();
}
#endregion
#region Aggregate Members
/// <summary>
/// Returns the Iterator for this aggregate
/// object.
/// </summary>
/// <returns>Iterator</returns>
public Iterator GetIterator()
{
return new IteratorImpl(this);
}
#endregion
}
#endregion
#region Iterator
interface Iterator
{
object First();
object Next();
bool IsDone();
object Current();
}
class IteratorImpl : Iterator
{
#region Memebrs
private readonly AggregateImpl _aggregate;
private int _nCurrentIndex;
#endregion
#region Iterator Members
/// <summary>
/// Return the first object of the iterator.
/// </summary>
/// <returns>First object of the iterator</returns>
public object First()
{
return _aggregate[0];
}
/// <summary>
/// Return the current object in the iterator and
/// advance to the next one.
/// </summary>
/// <returns>The next object in the iterator</returns>
public object Next()
{
object result = null;
if (_nCurrentIndex < _aggregate.Count - 1)
{
result = _aggregate[_nCurrentIndex];
_nCurrentIndex++;
}
return result;
}
/// <summary>
/// Returns true if the iteration is done.
/// </summary>
/// <returns>True if the iteration is done</returns>
public bool IsDone()
{
return _nCurrentIndex >= _aggregate.Count;
}
/// <summary>
/// Return the current object in the iterator.
/// </summary>
/// <returns></returns>
public object Current()
{
return _aggregate[_nCurrentIndex];
}
#endregion
#region Ctor
/// <summary>
/// Construct a new IteratorImpl with the given
/// aggregate.
/// </summary>
/// <param name="aggregate">The given aggregate</param>
public IteratorImpl(AggregateImpl aggregate)
{
_nCurrentIndex = 0;
_aggregate = aggregate;
}
#endregion
}
#endregion
There are 5 players in the example. The first player is an aggregate
item which is a simple data structure. We also have an aggregate
interface which has a GetIterator method that returns the iterator.
There is an Iterator interface that gives the guidelines of the
iterator behavior. I used the two interfaces to implement an
aggregate and an iterator.
// build a new string list
var strList = new List<string>
{
"str1",
"str2",
"str3"
};
// get list enumerator
IEnumerator<string> enumerator = strList.GetEnumerator();
// use the enumerator to traverse the list
// and output the list's items to console
string str;
while (enumerator.MoveNext())
{
str = enumerator.Current;
if (!string.IsNullOrEmpty(str))
{
Console.WriteLine("{0}", str);
}
}