<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 迭代模式
{
//定义得到开始对象,得到下一个对象,判断是否到结尾,当前对象等等抽象方法,统一接口。
abstract class Iterator
{
public abstract object First(); //定义得到开始对象
public abstract object Next(); //得到下一个对象
public abstract bool IsDone(); //判断是否到结尾,
public abstract object CurrentItem();//统一接口。
}
}</span>
<span style="font-size:18px;">//聚集抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 迭代模式
{
//创建迭代器
abstract class Aggregate
{
public abstract Iterator CreateIterator();
</span>
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 迭代模式
{
class ConcreteIterator : Iterator
{
//定义一个具体聚集对象
private ConcreteAggregate aggregate;
private int current = 0;
//初始化是将具体的聚集对象传人
public ConcreteIterator(ConcreteAggregate aggregate)
{
this.aggregate = aggregate;
}
//得到第一个对象
public override object First()
{
return aggregate[0]; //这个中括号在这什么意思?——这是得到一个聚集对象的方法
}
//得到聚集的下一个对象
public override object Next()
{
object ret = null; //ret 在这是什么意思?
current++;
if (current < aggregate.Count)
{
ret = aggregate[current];
}
return ret;
}
public override bool IsDone()
{
return current >= aggregate.Count ? true : false;
}
public override object CurrentItem()
{
return aggregate[current];
}
//判断当前师傅偏历到结尾,到结尾返回True
//public override bool IsDone();
//{
// return current > = aggregate.Count ? true : false;
//}
////返回当前聚集对象
//public override object CurrentItem()
//{
// return aggregate[current];
//}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 迭代模式
{
class ConcreteAggregate : Aggregate
{
//声明一个IList泛型变量,用于存放聚合对象,用ArrayList同样可以实现
private IList<object> items = new List<object>();
public override Iterator CreateIterator()
{
return new ConcreteIterator(this);
}
//返回聚集总个数
public int Count
{
get { return items.Count; }
}
//声明一个 索引器
public object this[int index]
{
get { return items[index]; }
set { items.Insert(index, value); }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 迭代模式
{
class Program
{
static void Main(string[] args)
{
ConcreteAggregate a = new ConcreteAggregate(); //公交车 聚集对象
//新上来乘客,即对象组
a[0] = "大鸟";
a[1] = "小菜";
a[2] = "行李";
a[3] = "公交内部员工";+
a[4] = "小偷";
Iterator i = new ConcreteIterator(a); //售票员处处,先看好上车是哪些人,也即是声明迭代对象
object item = i.First();
while (!i.IsDone()) //从第一个乘客开始
{
Console.WriteLine("{0} 请买车票!",i.CurrentItem());
i.Next();
}
Console.Read();
}
}
}
<span style="font-size:18px;">a[1] = "大鸟"; a[2] = "小菜"; a[3] = "行李"; a[4] = "公交内部员工"; a[5] = "小偷";</span>
<span style="font-size:18px;">a[0] = "大鸟"; a[1] = "小菜"; a[2] = "行李"; a[3] = "公交内部员工"; a[4] = "小偷";</span>
<span style="font-size:18px;"> Console.WriteLine("{0} 请买车票!",i.CurrentItem());</span>版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/u013067756/article/details/48005393