Iterator:
Provides a way to sequentially access aggregate objects without
exposing the
structure of the aggregate.
class Program
{
static void Main(string[] args)
{
IAggregate<string> aggregate = new ConcreteAggregate<string>();
aggregate.AddItem("Apple");
aggregate.AddItem("Mango");
foreach (string i in aggregate.GetAll())
Console.WriteLine(i);
}
}
public interface IAggregate<t>
{
IIterator<t> CreateIterator();
List<t> GetAll();
void AddItem(t item);
}
public interface IIterator<t>
{
t First();
t Next();
t CurrentItem();
bool IsDone();
void AddItem(t item);
}
public class ConcreteAggregate<t> : IAggregate<t>
{
private IIterator<t> iterator;
public ConcreteAggregate()
{
(this as IAggregate<t>).CreateIterator();
}
public IIterator<t> CreateIterator()
{
if (iterator != null)
iterator = new ConcreteIterator<t>(this);
return iterator;
}
public List<t> GetAll()
{
List<t> list = new List<t>();
list.Add(iterator.First());
while (!iterator.IsDone())
{
list.Add(iterator.Next());
}
return list;
}
public void AddItem(t item)
{
iterator.AddItem(item);
}
}
public class ConcreteIterator<t> : IIterator<t>
{
private IAggregate<t> aggregate;
private List<t> collection = new List<t>();
private int pointer = 0;
public ConcreteIterator(IAggregate<t> i)
{
aggregate = i;
}
public t First()
{
pointer = 0;
return collection[pointer];
}
public t Next()
{
pointer++;
return collection[pointer];
}
public t CurrentItem()
{
return collection[pointer];
}
public bool IsDone()
{
return pointer == collection.Count - 1;
}
public void AddItem(t item)
{
collection.Add(item);
}
}
class Program
{
static void Main(string[] args)
{
IAggregate<string> aggregate = new ConcreteAggregate<string>();
aggregate.AddItem("Apple");
aggregate.AddItem("Mango");
foreach (string i in aggregate.GetAll())
Console.WriteLine(i);
}
}
public interface IAggregate<t>
{
IIterator<t> CreateIterator();
List<t> GetAll();
void AddItem(t item);
}
public interface IIterator<t>
{
t First();
t Next();
t CurrentItem();
bool IsDone();
void AddItem(t item);
}
public class ConcreteAggregate<t> : IAggregate<t>
{
private IIterator<t> iterator;
public ConcreteAggregate()
{
(this as IAggregate<t>).CreateIterator();
}
public IIterator<t> CreateIterator()
{
if (iterator != null)
iterator = new ConcreteIterator<t>(this);
return iterator;
}
public List<t> GetAll()
{
List<t> list = new List<t>();
list.Add(iterator.First());
while (!iterator.IsDone())
{
list.Add(iterator.Next());
}
return list;
}
public void AddItem(t item)
{
iterator.AddItem(item);
}
}
public class ConcreteIterator<t> : IIterator<t>
{
private IAggregate<t> aggregate;
private List<t> collection = new List<t>();
private int pointer = 0;
public ConcreteIterator(IAggregate<t> i)
{
aggregate = i;
}
public t First()
{
pointer = 0;
return collection[pointer];
}
public t Next()
{
pointer++;
return collection[pointer];
}
public t CurrentItem()
{
return collection[pointer];
}
public bool IsDone()
{
return pointer == collection.Count - 1;
}
public void AddItem(t item)
{
collection.Add(item);
}
}
No comments:
Post a Comment