我读设计模式之Iterator模式
Iterator模式
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IteratorPattern
{
/// <summary>
/// 一般的Iterator模式实现
/// </summary>
class Program
{
static void Main(string[] args)
{
IIterator iterator;
IList list = new ConCreteList();
iterator = list.GetIterator();
while (iterator.MoveNext())
{
int i = (int)iterator.CurrentItem();
Console.WriteLine(i.ToString());
iterator.Next();
}
Console.Read();
}
}
/// <summary>
/// 抽象聚集
/// </summary>
public interface IList
{
IIterator GetIterator();
}
/// <summary>
/// 抽象迭代器
/// </summary>
public interface IIterator
{
bool MoveNext();
object CurrentItem();
void First();
void Next();
}
/// <summary>
/// 具体聚集
/// </summary>
public class ConCreteList : IList
{
int[] list;
public ConCreteList()
{
list = new int[] { 1, 2, 3, 4, 5 };
}
public IIterator GetIterator()
{
return new ConcreteIterator(this);
}
public int Length
{
get { return list.Length; }
}
public int GetElement(int index)
{
return list[index];
}
}
/// <summary>
/// 具体迭代器
/// </summary>
public class ConcreteIterator : IIterator
{
private ConCreteList list;
private int index;
public ConcreteIterator(ConCreteList list)
{
this.list = list;
index = 0;
}
public bool MoveNext()
{
if (index < list.Length)
{
return true;
}
else
{
return false;
}
}
public object CurrentItem()
{
return list.GetElement(index);
}
public void First()
{
index = 0;
}
public void Next()
{
if (index < list.Length)
{
index++;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IteratorPattern
{
/// <summary>
/// 一般的Iterator模式实现
/// </summary>
class Program
{
static void Main(string[] args)
{
IIterator iterator;
IList list = new ConCreteList();
iterator = list.GetIterator();
while (iterator.MoveNext())
{
int i = (int)iterator.CurrentItem();
Console.WriteLine(i.ToString());
iterator.Next();
}
Console.Read();
}
}
/// <summary>
/// 抽象聚集
/// </summary>
public interface IList
{
IIterator GetIterator();
}
/// <summary>
/// 抽象迭代器
/// </summary>
public interface IIterator
{
bool MoveNext();
object CurrentItem();
void First();
void Next();
}
/// <summary>
/// 具体聚集
/// </summary>
public class ConCreteList : IList
{
int[] list;
public ConCreteList()
{
list = new int[] { 1, 2, 3, 4, 5 };
}
public IIterator GetIterator()
{
return new ConcreteIterator(this);
}
public int Length
{
get { return list.Length; }
}
public int GetElement(int index)
{
return list[index];
}
}
/// <summary>
/// 具体迭代器
/// </summary>
public class ConcreteIterator : IIterator
{
private ConCreteList list;
private int index;
public ConcreteIterator(ConCreteList list)
{
this.list = list;
index = 0;
}
public bool MoveNext()
{
if (index < list.Length)
{
return true;
}
else
{
return false;
}
}
public object CurrentItem()
{
return list.GetElement(index);
}
public void First()
{
index = 0;
}
public void Next()
{
if (index < list.Length)
{
index++;
}
}
}
}
Code
using System;
using System.Collections;
namespace IteratorPattern
{
/// <summary>
/// .NET 1.1 中的Iterator模式
/// </summary>
class Demo1
{
static void Main()
{
Info[] list = new Info[] { new Info { Name = "ivan" }, new Info { Name = "ddd" } };
CrecerateList lists = new CrecerateList(list);
foreach (Info s in lists)
{
Console.WriteLine(s.Name);
}
}
}
public class Info
{
public string id { set; get; }
public string Name
{
set;
get;
}
}
public class CrecerateList : IEnumerable
{
Info[] list;
public CrecerateList(Info[] list)
{
this.list = list;
}
public IEnumerator GetEnumerator()
{
return new CrecerateRator(list);
}
}
public class CrecerateRator : IEnumerator
{
Info[] _infos;
int index = -1;
public CrecerateRator(Info[] infos)
{
this._infos = infos;
}
public bool MoveNext()
{
index++;
if (index < _infos.Length)
{
return true;
}
else
{
return false;
}
}
public void Reset()
{
index = -1;
}
public object Current
{
get
{
return _infos[index];
}
}
}
}
using System;
using System.Collections;
namespace IteratorPattern
{
/// <summary>
/// .NET 1.1 中的Iterator模式
/// </summary>
class Demo1
{
static void Main()
{
Info[] list = new Info[] { new Info { Name = "ivan" }, new Info { Name = "ddd" } };
CrecerateList lists = new CrecerateList(list);
foreach (Info s in lists)
{
Console.WriteLine(s.Name);
}
}
}
public class Info
{
public string id { set; get; }
public string Name
{
set;
get;
}
}
public class CrecerateList : IEnumerable
{
Info[] list;
public CrecerateList(Info[] list)
{
this.list = list;
}
public IEnumerator GetEnumerator()
{
return new CrecerateRator(list);
}
}
public class CrecerateRator : IEnumerator
{
Info[] _infos;
int index = -1;
public CrecerateRator(Info[] infos)
{
this._infos = infos;
}
public bool MoveNext()
{
index++;
if (index < _infos.Length)
{
return true;
}
else
{
return false;
}
}
public void Reset()
{
index = -1;
}
public object Current
{
get
{
return _infos[index];
}
}
}
}
Code
using System;
using System.Collections;
namespace IteratorPattern
{
/// <summary>
/// .NET 2.0 中的Iterator模式: yield关键字
/// </summary>
class Demo2
{
static void Main()
{
Info[] list = new Info[] { new Info { Name = "ivan" }, new Info { Name = "Ashley" } };
CrecerateList lists = new CrecerateList(list);
foreach (Info s in lists)
{
Console.WriteLine(s.Name);
}
}
}
public class CrecerateList : IEnumerable
{
Info[] list;
public CrecerateList(Info[] list)
{
this.list = list;
}
public IEnumerator GetEnumerator()
{
foreach (Info s in list)
{
yield return s;
}
}
}
public class Info
{
public string id { set; get; }
public string Name
{
set;
get;
}
}
}
using System;
using System.Collections;
namespace IteratorPattern
{
/// <summary>
/// .NET 2.0 中的Iterator模式: yield关键字
/// </summary>
class Demo2
{
static void Main()
{
Info[] list = new Info[] { new Info { Name = "ivan" }, new Info { Name = "Ashley" } };
CrecerateList lists = new CrecerateList(list);
foreach (Info s in lists)
{
Console.WriteLine(s.Name);
}
}
}
public class CrecerateList : IEnumerable
{
Info[] list;
public CrecerateList(Info[] list)
{
this.list = list;
}
public IEnumerator GetEnumerator()
{
foreach (Info s in list)
{
yield return s;
}
}
}
public class Info
{
public string id { set; get; }
public string Name
{
set;
get;
}
}
}