abstract class Iterator
{
public abstract object First();
public abstract object next();
public abstract bool IsDone();
public abstract object CurrentItem();
}
abstract class Aggregate
{
public abstract Iterator CreateIterator();
}
class ConcreteAggregate : Aggregate
{
private IList<object> items = new List<object>();
public override Iterator CreateIterator()
{
return new ConcreteIteator(this);
}
public int Count
{
get => items.Count;
}
public object this[int index]
{
get => items[index];
set => items.Insert(index, value);
}
}
class ConcreteIteator : Iterator
{
private ConcreteAggregate aggregate;
private int current = 0;
public ConcreteIteator(ConcreteAggregate concreteAggregate)
{
this.aggregate = concreteAggregate;
}
public override object CurrentItem()
{
return aggregate[current];
}
public override object First()
{
return aggregate[0];
}
public override bool IsDone()
{
return current >= aggregate.Count ? true : false;
}
public override object next()
{
object ret=null;
current++;
if(current<aggregate.Count)
{
ret = aggregate[current];
}
return ret;
}
}
class Program
{
static void Main(string[] args)
{
ConcreteAggregate concreteAggregate = new ConcreteAggregate();
concreteAggregate[0] = "1";
concreteAggregate[1] = "2";
concreteAggregate[2] = 4;
Iterator iterator = concreteAggregate.CreateIterator();
object item = iterator.First();
while(iterator.IsDone()==false)
{
Console.WriteLine(item);
item = iterator.next();
}
}
}