C#里面的
foreach (Component c in child)
{
c.Depth(depth + 2);
}
代码实例一
迭代器抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Iterator
{
abstract class Iterator
{
public abstract Object First();
public abstract Object Next();
public abstract bool IsDone();
public abstract Object CurrentItem();
}
}
聚集抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Iterator
{
abstract class Aggregate
{
public abstract Iterator CreateIterator();
}
}
具体迭代器类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Iterator
{
class ConcreteIterator : Iterator
{
private ConcreteAggregate concreteAggregate;
private int current = 0;
public ConcreteIterator(ConcreteAggregate concreteAggregate)
{
this.concreteAggregate = concreteAggregate;
}
public override object CurrentItem()
{
return concreteAggregate[current];
}
public override object First()
{
return concreteAggregate[0];
}
public override bool IsDone()
{
return current >= concreteAggregate.Count ? true : false;
}
public override object Next()
{
Object ret = null;
current++;
if(current<concreteAggregate.Count)
{
ret = concreteAggregate[current];
}
return ret;
}
}
}
从后往前遍历
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Iterator
{
class ConcreteIteratorDesc : Iterator
{
private ConcreteAggregate concreteAggregate;
private int current = 0;
public ConcreteIteratorDesc(ConcreteAggregate concreteAggregate)
{
this.concreteAggregate = concreteAggregate;
current = concreteAggregate.Count - 1;
}
public override object CurrentItem()
{
return concreteAggregate[current];
}
public override object First()
{
return concreteAggregate[concreteAggregate.Count - 1];
}
public override bool IsDone()
{
return current < 0 ? true : false;
}
public override object Next()
{
object ret = null;
current--;
if(current>=0)
{
ret = concreteAggregate[current];
}
return ret;
}
}
}
具体聚集类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Iterator
{
class ConcreteAggregate : Aggregate
{
private List<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.Threading.Tasks;
using System.Windows.Forms;
namespace Iterator
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
ConcreteAggregate a = new ConcreteAggregate();
a[0] = "1";
a[1] = "2";
a[2] = "3";
Iterator i = new ConcreteIterator(a);
Object item = i.First();
while(!i.IsDone())
{
Console.WriteLine(i.CurrentItem());
i.Next();
}
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
}
}
}