迭代器详解

迭代器模式

定义

迭代器提供了一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部结构。

结构

1

迭代器由四部分组成:

  1. Iterator:定义访问和遍历元素的接口。
  2. ConcreteIterator:实现迭代器接口;遍历时跟踪当前位置。
  3. Aggregate:定义创建相应迭代器对象的接口。
  4. ConCreteAggregate:实现创建Aggregate中定义的接口。

适用场景

  1. 系统需要访问一个聚合对象的内容而无需暴露它的内部表示
  2. 系统需要支持对聚合对象的多种遍历
  3. 系统需要为不同的聚合结构提供一个统一的接口

实现一个简单的迭代器

 下边是一个简单的Iterator的实现

class ListIterator<T> : IEnumerator<T>
    {
        List<T> list;
        int current;

        public ListIterator(List<T> list)
        {
            this.list = list;
            current = -1;
        }

        public T Current { get { return list[current]; } }

        public bool MoveNext()
        {
            return ++current < list.Count;
        }

        public void Reset()
        {
            current = -1;
        }

    }

虽然我们不能修改list本身,但是我们可以使用ListIterable<T>封装一个list:

 1 class ListIterator<T> : IEnumerator<T>
 2     {
 3         List<T> list;
 4         int current;
 5 
 6         public ListIterator(List<T> list)
 7         {
 8             this.list = list;
 9             current = -1;
10         }
11 
12         public T Current { get { return list[current]; } }
13 
14         public bool MoveNext()
15         {
16             return ++current < list.Count;
17         }
18 
19         public void Reset()
20         {
21             current = -1;
22         }
23 
24     }


最后我们可以写一个返回Ienumerable<T>类型的GetElements方法

static IEnumerable<T> GetElements<T>(this List<T> list)
{
  return new ListIterable<T>(list);
}

 

C#中的迭代器 

.NET中迭代器模式的IteratorAggregate已经存在了,其中IEnumerator接口就是Iterator,而IEnumerable就是Aggregate。不仅如此,在.NET2.0之后的版本中,我们可以使用yield关键字方便的实现迭代器模式。

例如上边GetElements方法的代码可以写成:

        static IEnumerable<T> GetElements<T>(List<T> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                yield return list[i];
            }
        }

 

转载于:https://www.cnblogs.com/ldm1989/p/4170696.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值