Design Pattern : Iterator
Definition :
「 Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.」
「 Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.」
1. IIterator
: 提供存放集合,以及操作、尋覽集合的方法。
2. IAggregate:將Iterator封裝給Client使用的interface。
3. ConcreteAggregate
依賴於 ConcreteIterator,ConcreteIterator則關聯ConcreteIterator。
程式碼:
1.
IIterator
public interface IIterator<T>
{
/// 取得第一個元素
T First();
/// 取得下一個元素
T Next();
/// 目前尋覽的元素
T CurrentItem();
/// 檢查是否已循覽至最後一個元素
bool IsDone();
/// 加入元素
void AddItem(T item);
}
public interface IIterator<T>
{
/// 取得第一個元素
T First();
/// 取得下一個元素
T Next();
/// 目前尋覽的元素
T CurrentItem();
/// 檢查是否已循覽至最後一個元素
bool IsDone();
/// 加入元素
void AddItem(T item);
}
2. IAggregate
public interface IAggregate<T>
{
/// 建立IIterator物件
IIterator<T> CreateIterator();
/// 尋覽(取得)所有元素
List<T> GetAll();
/// 加入元素
void AddItem(T item);
}
public interface IAggregate<T>
{
/// 建立IIterator物件
IIterator<T> CreateIterator();
/// 尋覽(取得)所有元素
List<T> GetAll();
/// 加入元素
void AddItem(T item);
}
3. ConcreteIterator
=> 實際存放元素集合的物件
=> 紀錄封裝本身的物件:IAggregate<T>
=> 利用一個pointer去記錄目前尋覽第幾個元素
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> item)
{
this.aggregate = item;
}
public T First()
{
this.pointer = 0;
return this.collection[this.pointer];
}
public T Next()
{
this.pointer++;
return this.collection[this.pointer];
}
public T CurrentItem()
{
return this.collection[this.pointer];
}
/// 檢查是否已循覽至最後一個元素
public bool IsDone()
{
bool isDone = false;
if(this.pointer == collection.Count-1)
{ isDone = true; }
return isDone;
}
public void AddItem(T item)
{
this.collection.Add(item);
}
}
=> 實際存放元素集合的物件
=> 紀錄封裝本身的物件:IAggregate<T>
=> 利用一個pointer去記錄目前尋覽第幾個元素
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> item)
{
this.aggregate = item;
}
public T First()
{
this.pointer = 0;
return this.collection[this.pointer];
}
public T Next()
{
this.pointer++;
return this.collection[this.pointer];
}
public T CurrentItem()
{
return this.collection[this.pointer];
}
/// 檢查是否已循覽至最後一個元素
public bool IsDone()
{
bool isDone = false;
if(this.pointer == collection.Count-1)
{ isDone = true; }
return isDone;
}
public void AddItem(T item)
{
this.collection.Add(item);
}
}
4.
ConcreteAggregate
=> Create並存放IIterator物件
=> 封裝IIterator相關方法,提供GetAll方法。
public class ConcreteAggregate<T> : IAggregate<T>
{
private IIterator<T> iterator;
public ConcreteAggregate()
{
this.CreateIterator();
}
public IIterator<T> CreateIterator()
{
if (this.iterator == null)
{
this.iterator = new ConcreteIterator<T>(this);
}
return this.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);
}
}
=> Create並存放IIterator物件
=> 封裝IIterator相關方法,提供GetAll方法。
public class ConcreteAggregate<T> : IAggregate<T>
{
private IIterator<T> iterator;
public ConcreteAggregate()
{
this.CreateIterator();
}
public IIterator<T> CreateIterator()
{
if (this.iterator == null)
{
this.iterator = new ConcreteIterator<T>(this);
}
return this.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);
}
}
5. 主程式:
IAggregate<ICar> aggregate = new ConcreteAggregate<ICar>();
aggregate.AddItem(new Focus());
aggregate.AddItem(new Civic());
aggregate.AddItem(new V60());
//Use IAggregate to iterate through the collection
foreach (ICar _car in aggregate.GetAll())
Console.WriteLine(_car.Name);
結果:
Focus
Civic
V60
IAggregate<ICar> aggregate = new ConcreteAggregate<ICar>();
aggregate.AddItem(new Focus());
aggregate.AddItem(new Civic());
aggregate.AddItem(new V60());
//Use IAggregate to iterate through the collection
foreach (ICar _car in aggregate.GetAll())
Console.WriteLine(_car.Name);
結果:
Focus
Civic
V60
6. Reference
Iterator Design Pattern (CodeProject)
Iterator Design Pattern (CodeProject)
沒有留言:
張貼留言