迭代器模式:提供一种方法顺序访问一个聚合对象中的各个元素,而有不暴露起内部的表示。 【Head First design pattern】
下面来分析一个Demo:
这个就是"聚合对象中的元素"。
ClassA 中使用数组来存储“元素”。
ClassB 中使用List来存储“元素”。
测试代码:
现在我们看看用迭代器模式怎么做:
1.定义接口:
classA对应的迭代器。
下面来分析一个Demo:
这个就是"聚合对象中的元素"。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
class
ItemFir:IItem

{
string name;
ItemFlag flag;

public ItemFlag Flag

{

get
{ return flag; }

set
{ flag = value; }
}
public ItemFir(string name, string des,ItemFlag itemflag)

{
this.des = des;
this.name = name;
this.flag = itemflag;
}
public string Name

{

get
{ return name; }

set
{ name = value; }
}

string des;

public string Des

{

get
{ return des; }

set
{ des = value; }
}
}





















































ClassA 中使用数组来存储“元素”。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
class
ClassA

{
IItem[] myitemlist = new ItemFir[10];

#region
public ClassA()

{
ItemFlag flag = ItemFlag.LOW;
for (int i = 0; i < myitemlist.Length; i++)

{
flag = (ItemFlag)System.Enum.Parse(typeof(ItemFlag), "" + (i - (myitemlist.Length / 2)) % 2 + "");
// 获得 -1,0,1对应的枚举
myitemlist[i] = new ItemFir(String.Format("ClassA{0}",i),String.Format("ClassA{0}Des",i),flag);
}
}
public string GetDes()

{
return "I'am ClassA";
}
public void Add(IItem item)

{
}
public void Remove(IItem item)

{
}
public IItem Get(int i)

{
return myitemlist[i];
}
#endregion
public IItem[] GetAll()

{
return myitemlist;
}
//public IIterator CreateIterator()
//{
// return new ClassAIterator(myitemlist);
//}
}
























































ClassB 中使用List来存储“元素”。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
class
ClassB

{
List<IItem> myitemlist = new List<IItem>();

#region
public ClassB()

{
myitemlist.Add(new ItemFir("ClassB Item1", "ClassB Item1 Des", ItemFlag.HIGH));
myitemlist.Add(new ItemFir("ClassB Item2", "ClassB Item3 Des", ItemFlag.LOW));
myitemlist.Add(new ItemFir("ClassB Item3", "ClassB Item3 Des", ItemFlag.HIGH));
myitemlist.Add(new ItemFir("ClassB Item4", "ClassB Item4 Des", ItemFlag.MEDIAL));
myitemlist.Add(new ItemFir("ClassB Item5", "ClassB Item5 Des", ItemFlag.LOW));
}
public string GetDes()

{
return "I'am ClassB";
}

public void AddItem(IItem item)

{
}
public void RemoveItem(IItem item)

{
}
public IItem GetItem(int i)

{
return myitemlist[i];
}
#endregion
public List<IItem> GetAllItem()

{
return myitemlist;
}
//public IIterator CreateIterator()
//{
// return new ClassBIterator(myitemlist);
//}
}
ItemFlag 源码:




















































<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
enum
ItemFlag

{
LOW = -1,
MEDIAL = 0,
HIGH = 1,
}








测试代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
static
void
Main(
string
[] args)

{
ClassA classA = new ClassA();
IItem[] itemlistA = classA.GetAll();
Console.Write("ClassA\n");


for (int i = 0; i < itemlistA.Length; i++)

{
if (itemlistA[i].Flag == ItemFlag.LOW)
Console.Write(itemlistA[i].Name + " " + itemlistA[i].Flag + "\n");
}
Console.Write("ClassB\n");
ClassB classB = new ClassB();
List<IItem> itemlistB = classB.GetAllItem();

for (int j = 0; j < itemlistB.Count; j++)

{
if (itemlistB[j].Flag == ItemFlag.LOW)
Console.Write(itemlistB[j].Name + " " + itemlistB[j].Flag + "\n");
}
}
我们希望把 ClassA 中和ClassB 中所有ItemFalg 值为LOW的元素选择出来。其实对于客户来说没必要知道你用数组还是list来保存的。客户需要用不同的方式来访问聚合,也显得不够优雅。(
itemlistB.Count ,
itemlistA.Length)还有像上面如果有10个类就得用十个循环。



























现在我们看看用迭代器模式怎么做:
1.定义接口:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
interface
IIterator

{
bool HasNext();
IItem Next();
}
2. 实现迭代器:








classA对应的迭代器。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
class
ClassAIterator : IIterator

{
int posion;
IItem[] item;
public ClassAIterator(IItem[] item)

{
this.item = item;
}

public bool HasNext()

{
return posion <= item.Length - 1;
}

public IItem Next()

{
return item[posion++];
}

}
ClassB的迭代器:




























<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
class ClassBIterator:IIterator

{

int posion;
List< IItem> item;
public ClassBIterator( List< IItem> item)



<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->









