天天看点

(41)C#设计模式——迭代器模式(Iterator Pattern)

介绍

迭代器是针对集合对象而产生的,对于 集合对象而言,必然涉及到集合元素的添加、删除等操作,同时肯定也支持遍历集合元素的操作,我们可以把遍历操作也放在集合对象中,但这样的话,集合对象就承担太多的责任了,面向对象中有一条设计原则是单一职责原则,所以我们要尽可能的分离这些职责,用不同的类去承担不同的责任。迭代器模式就是来承担遍历集合元素的职责。

定义

迭代器模式提供了一种方法顺序访问一个聚合对象(理解为集合对象)中各个元素,而又无需暴露该对象的内部表示,这样既可以做到不暴露集合的内部结构,又可以让外部代码透明地访问集合内部数据。

结构

既然,迭代器模式承担了遍历集合对象的职责,则该模式自然存在两个类,一个是聚合类,一个是迭代器类。在面向对象设计原则中还有一条是针对接口编程,所以在迭代器模式中,抽象了两个接口,一个是聚合接口,另一个是迭代器接口,这样迭代器模式就有四个角色了:

(41)C#设计模式——迭代器模式(Iterator Pattern)

迭代器角色(Iterator):迭代器角色负责定义访问和遍历集合元素的接口

具体迭代器角色(ConcreteIterator):具体迭代器角色实现了迭代器接口,并需要记录遍历中的当前位置

聚合角色(Aggregate):聚合角色负责定义获得迭代器角色的接口

具体聚合角色(ConcreteAggregate):具体聚合角色实现聚合角色的接口

实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _31IteratorPatternDemo
{
    //抽象聚合类
    public interface IListCollection
    {
        Iterator GetInerator();
    }
    //迭代器抽象类
    public interface Iterator
    {
        bool MoveNext();
        Object GetCurrent();
        void Next();
        void Reset();
    }
    //具体聚合类
    public class ConcreteList : IListCollection
    {
        int[] collection;
        public ConcreteList()
        {
            collection = new int[] { 2, 4, 6, 8 };
        }
        public Iterator GetInerator()
        {
            return new ConcreteIterator(this);
        }
        public int Length
        {
            get { return collection.Length; }
        }
        public int GetElement(int index)
        {
            return collection[index];
        }
    }
    //具体迭代器类
    public class ConcreteIterator : Iterator
    {
        //迭代器要集合对象进行遍历操作,自然就需要引用集合对象
        private ConcreteList _list;
        private int _index;
        public ConcreteIterator(ConcreteList list)
        {
            _list = list;
            _index = 0;
        }

        public bool MoveNext()
        {
            if (_index < _list.Length)
            {
                return true;
            }
            return false;
        }
        public Object GetCurrent()
        {
            return _list.GetElement(_index);
        }
        public void Reset()
        {
            _index = 0;
        }
        public void Next()
        {
            if (_index < _list.Length)
            {
                _index++;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Iterator iterator;
            IListCollection list = new ConcreteList();
            iterator = list.GetInerator();

            while (iterator.MoveNext())
            {
                int i = (int)iterator.GetCurrent();
                Console.WriteLine(i.ToString());
                iterator.Next();
            }
        }
    }
}
           

适用场景

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

优缺点

优点:

  • 迭代器模式使得访问一个聚合对象的内容而无需暴露它的内部表示,即迭代抽象
  • 迭代器模式为遍历不同的集合结构提供了一个统一的接口,从而支持同样的算法在不同的集合结构上进行操作

缺点:

  • 迭代器模式在遍历的同时更改迭代器所在的结合结构会导致出现异常。所以使用foreach语句只能对集合进行遍历,不能在遍历的同时更改集合中的元素

总结

迭代器模式就是抽象一个迭代器类来分离了集合对象的遍历行为,这样既可以做到不暴露集合的内部结构,又可以让外部代码透明地访问集合内部的数据。