迭代器模式(Iterator)
来源:互联网 发布:淘宝定制店在哪里 编辑:程序博客网 时间:2024/04/30 08:26
1. 定义
提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露该对象的内部表示。2. UML 类图
3. 结构代码
using System;
using System.Collections;
namespace DoFactory.GangOfFour.Iterator.Structural
{
///<summary>
/// MainApp startup class for Structural
/// Iterator Design Pattern.
///</summary>
classMainApp
{
///<summary>
/// Entry point into console application.
///</summary>
staticvoid Main()
{
ConcreteAggregate a = new ConcreteAggregate();
a[0] = "Item A";
a[1] = "Item B";
a[2] = "Item C";
a[3] = "Item D";
// Create Iterator and provide aggregate
ConcreteIterator i = new ConcreteIterator(a);
Console.WriteLine("Iterating over collection:");
object item = i.First();
while (item != null)
{
Console.WriteLine(item);
item = i.Next();
}
// Wait for user
Console.ReadKey();
}
}
///<summary>
/// The 'Aggregate' abstract class
///</summary>
abstractclass Aggregate
{
publicabstract Iterator CreateIterator();
}
///<summary>
/// The 'ConcreteAggregate' class
///</summary>
classConcreteAggregate : Aggregate
{
privateArrayList _items = newArrayList();
publicoverride Iterator CreateIterator()
{
return new ConcreteIterator(this);
}
// Gets item count
publicint Count
{
get { return _items.Count; }
}
// Indexer
publicobject this[int index]
{
get { return _items[index]; }
set { _items.Insert(index, value); }
}
}
///<summary>
/// The 'Iterator' abstract class
///</summary>
abstractclass Iterator
{
publicabstract object First();
publicabstract object Next();
publicabstract bool IsDone();
publicabstract object CurrentItem();
}
///<summary>
/// The 'ConcreteIterator' class
///</summary>
classConcreteIterator : Iterator
{
privateConcreteAggregate _aggregate;
privateint _current = 0;
// Constructor
public ConcreteIterator(ConcreteAggregate aggregate)
{
this._aggregate = aggregate;
}
// Gets first iteration item
publicoverride object First()
{
return _aggregate[0];
}
// Gets next iteration item
publicoverride object Next()
{
object ret = null;
if (_current < _aggregate.Count - 1)
{
ret = _aggregate[++_current];
}
return ret;
}
// Gets current iteration item
publicoverride object CurrentItem()
{
return _aggregate[_current];
}
// Gets whether iterations are complete
publicoverride bool IsDone()
{
return _current >= _aggregate.Count;
}
}
}
Output
Item A
Item B
Item C
Item D
4. 实例代码
using System;
using System.Collections;
namespace DoFactory.GangOfFour.Iterator.RealWorld
{
///<summary>
/// MainApp startup class for Real-World
/// Iterator Design Pattern.
///</summary>
classMainApp
{
///<summary>
/// Entry point into console application.
///</summary>
staticvoid Main()
{
// Build a collection
Collection collection = new Collection();
collection[0] = new Item("Item 0");
collection[1] = new Item("Item 1");
collection[2] = new Item("Item 2");
collection[3] = new Item("Item 3");
collection[4] = new Item("Item 4");
collection[5] = new Item("Item 5");
collection[6] = new Item("Item 6");
collection[7] = new Item("Item 7");
collection[8] = new Item("Item 8");
// Create iterator
Iterator iterator = new Iterator(collection);
// Skip every other item
iterator.Step = 2;
Console.WriteLine("Iterating over collection:");
for (Item item = iterator.First();
!iterator.IsDone; item = iterator.Next())
{
Console.WriteLine(item.Name);
}
// Wait for user
Console.ReadKey();
}
}
///<summary>
/// A collection item
///</summary>
classItem
{
privatestring _name;
// Constructor
public Item(string name)
{
this._name = name;
}
// Gets name
publicstring Name
{
get { return _name; }
}
}
///<summary>
/// The 'Aggregate' interface
///</summary>
interfaceIAbstractCollection
{
Iterator CreateIterator();
}
///<summary>
/// The 'ConcreteAggregate' class
///</summary>
classCollection : IAbstractCollection
{
privateArrayList _items = newArrayList();
publicIterator CreateIterator()
{
return new Iterator(this);
}
// Gets item count
publicint Count
{
get { return _items.Count; }
}
// Indexer
publicobject this[int index]
{
get { return _items[index]; }
set { _items.Add(value); }
}
}
///<summary>
/// The 'Iterator' interface
///</summary>
interfaceIAbstractIterator
{
Item First();
Item Next();
bool IsDone {get; }
Item CurrentItem { get; }
}
///<summary>
/// The 'ConcreteIterator' class
///</summary>
classIterator : IAbstractIterator
{
privateCollection _collection;
privateint _current = 0;
privateint _step = 1;
// Constructor
public Iterator(Collection collection)
{
this._collection = collection;
}
// Gets first item
publicItem First()
{
_current = 0;
return _collection[_current] as Item;
}
// Gets next item
publicItem Next()
{
_current += _step;
if (!IsDone)
return _collection[_current] as Item;
else
return null;
}
// Gets or sets stepsize
publicint Step
{
get { return _step; }
set { _step = value; }
}
// Gets current iterator item
publicItem CurrentItem
{
get { return _collection[_current] as Item; }
}
// Gets whether iteration is complete
publicbool IsDone
{
get { return _current >= _collection.Count; }
}
}
}
Output
Item 0
Item 2
Item 4
Item 6
Item 8
该文章来自:http://www.dofactory.com/Patterns/PatternIterator.aspx
- 迭代器模式(Iterator)
- 迭代器模式(Iterator)
- 迭代器模式(Iterator)
- Iterator(迭代器)模式
- 迭代器模式(Iterator)
- 迭代器(Iterator)模式
- 迭代器模式(Iterator)
- Iterator(迭代器模式)
- 迭代器模式(Iterator)
- 迭代器模式(Iterator)
- 迭代器模式(Iterator)
- 迭代器模式(Iterator)
- 迭代器模式(Iterator)
- 迭代器模式(Iterator)
- 迭代器模式(Iterator)
- 迭代器模式(Iterator)
- 迭代器模式(Iterator)
- 迭代器模式(Iterator)
- 1290 献给杭电五十周年校庆的礼物
- 关于Android模拟器键盘不能使用的解决方法
- hdoj 1070
- hdoj 1290
- Java中使用MongoDB连接池的注意事项
- 迭代器模式(Iterator)
- 在iOS项目中使用CorePlot框架
- 说SQL中的TRUNCATE和DELETE 之间的区别
- hdoj 2537
- C函数中指针参数
- hdoj2547
- GDI+ 保存指定的窗口为JPEG文件(X Y轴镜像功能可选)
- 解题思路
- 刚开始职场生涯