迭代器模式

引言

在面向对象编程中,经常遇到集合对象,在对其操作时重点工作有:1、集合内部数据的存储,2、集合内部数据的查询。在面向对象设计原则中有一条是单一职责原则,所以要分离这些职责,用不同的类承担不同的责任。迭代器就是针对集合产生的,用来遍历集合元素的职责。

迭代器模式

  • 定义
    提供一种顺序访问一个聚合对象中各个元素,而又不暴漏该对象集合内部数据结构的方法。
  • 架构图
    架构图
    如上图所示,在迭代器模式中,迭代器需要承担遍历集合对象的职责,所以将对象升级为个集合,也就是聚合类,为了遍历则为聚合类提供一个迭代器类。在针对接口编程中,可以为聚合接口、迭代器接口,再加上接口的实现,四个类就产生了。
    从上图可以看出,迭代器模式由以下角色组成:
    • 迭代器角色(Iterator):迭代器角色负责定义访问和遍历元素的接口
    • 具体迭代器角色(Concrete Iteraror):具体迭代器角色实现了迭代器接口,并需要记录遍历中的当前位置。
    • 聚合角色(Aggregate):聚合角色负责定义获得迭代器角色的接口
    • 具体聚合角色(Concrete Aggregate):具体聚合角色实现聚合角色接口。
      具体聚合角色依赖于具体迭代器(产生具体迭代器),同时具体迭代器调用具体聚合角色,对它进行迭代。

程序设计

实现对一个集合类的查询功能。

  1. 程序设计图
    迭代器模式设计架构图
  2. 程序代码
public interface IAggergate 
{
    IIterator CreateIterator();

}

public interface IIterator 
{
    bool HasNext();

    void Next();

    void First();

    object CueerntItem();

}

public class ConcreteAggergate : IAggergate
{
    int [] num;
    public ConcreteAggergate()
    {
         num = new int[]{1,2,3,4};
    }
    //提供聚合类中供遍历集合的长度
    public int Length()
    {
        return num.Length;
    }

    //提供遍历查询的值
    public int GetElement(int index)
    {
        return num[index];
    }
    public virtual IIterator CreateIterator()
    {
        return new ConcreteIterator(this);
    }

}
ublic class ConcreteIterator : IIterator
{
    int position = -1;
    //因为要对Concrete对象遍历
    ConcreteAggergate _list;
    //关键在迭代器中传入需要操作的对象,对象提供长度和值得函数,供调用。
    public ConcreteIterator(ConcreteAggergate list)
    {
        _list = list;
        position = 0;
    }

    public virtual bool HasNext()
    {
        if (position >= _list.Length())
        {
            return false;
        }
        else
            return true;

    }

    public virtual void Next()
    {
        if (position < _list.Length())
        {
            position++;
        }       
    }

    public virtual void First()
    {
        position = 0;
    }

    public virtual object CueerntItem()
    {
        return _list.GetElement(position);
    }

}

//程序调用程序

public class program
{
    public static void Main(string [] args)
    {
        IAggergate age = new ConcreteAggergate();
        IIterator tor = age.CreateIterator();
        while(tor.HasNext())
        {
            int i = (int)tor.CueerntItem();
            Console.WriteLine(i.ToString());
            tor.Next();
        }

    }
}
View Code

迭代器模式的实现

  • 为迭代器和聚类分别建立接口,以及接口的具体实现类
  • 迭代器接口有常用的查询、初始化等功能,聚类接口创建迭代器(就是将在具体实体类中将自己传入到迭代器中以供调用

    Net中迭代器模式的应用

在.NET下,迭代器模式中的聚集接口和迭代器接口都已经存在了,其中IEnumerator接口扮演的就是迭代器角色,IEnumberable接口则扮演的就是抽象聚集的角色

//IEnumable是个抽象聚类
public class Persons : IEnumerable 
{ 
    string[] m_Names; 
//只有类实现了IEnumerable才能在foreach调用。
    public Persons(params string[] Names) 
    { 
        m_Names = new string[Names.Length]; 

        Names.CopyTo(m_Names,0); 
    } 

    public IEnumerator GetEnumerator() 
    { 
        foreach (string s in m_Names) 
        { //通过yield自动实现了返回集合
            yield return s; 
        } 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
        Persons arrPersons = new Persons("Michel","Christine","Mathieu","Julien"); 

        foreach (string s in arrPersons) 
        { 
            Console.WriteLine(s); 
        }

        Console.ReadLine(); 
    } 
}
View Code

综述

代器模式就是抽象一个迭代器类来分离了集合对象的遍历行为,这样既可以做到不暴露集合的内部结构,又可让外部代码透明地访问集合内部的数据。迭代器模式使得访问一个聚合对象的内容而无需暴露它的内部表示,即迭代抽象。迭代器模式为遍历不同的集合结构提供了一个统一的接口,从而支持同样的算法在不同的集合结构上进行操作。

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/polly333/p/4705672.html