设计模式(15)迭代器模式

模式介绍

迭代器模式提供了一种访问底层表示中的对象的方法,而不会暴露对表示本身的访问。

示例

我们以果冻豆为例进行建模

/// <summary>
/// Our collection item.  Mostly because I'm a sucker for jelly beans.
/// </summary>
class JellyBean
{
    private string _flavor;

    // Constructor
    public JellyBean(string flavor)
    {
        this._flavor = flavor;
    }

        
    public string Flavor
    {
        get { return _flavor; }
    }
}

聚合接口和具体的聚合实现:

/// <summary>
/// The aggregate interface
/// </summary>
interface ICandyCollection
{
    IJellyBeanIterator CreateIterator();
}

/// <summary>
/// The ConcreteAggregate class
/// </summary>
class JellyBeanCollection : ICandyCollection
{
    private ArrayList _items = new ArrayList();

    public JellyBeanIterator CreateIterator()
    {
        return new JellyBeanIterator(this);
    }

    // Gets jelly bean count
    public int Count
    {
        get { return _items.Count; }
    }

    // Indexer
    public object this[int index]
    {
        get { return _items[index]; }
        set { _items.Add(value); }
    }
}

迭代器接口和具体的迭代器实现:

/// <summary>
/// The 'Iterator' interface
/// </summary>
interface IJellyBeanIterator
{
    JellyBean First();
    JellyBean Next();
    bool IsDone { get; }
    JellyBean CurrentBean { get; }
}

/// <summary>
/// The 'ConcreteIterator' class
/// </summary>
class JellyBeanIterator : IJellyBeanIterator
{
    private JellyBeanCollection _jellyBeans;
    private int _current = 0;
    private int _step = 1;

    // Constructor
    public JellyBeanIterator(JellyBeanCollection beans)
    {
        this._jellyBeans = beans;
    }

    // Gets first jelly bean
    public JellyBean First()
    {
        _current = 0;
        return _jellyBeans[_current] as JellyBean;
    }

    // Gets next jelly bean
    public JellyBean Next()
    {
        _current += _step;
        if (!IsDone)
            return _jellyBeans[_current] as JellyBean;
        else
            return null;
    }

    // Gets current iterator candy
    public JellyBean CurrentBean
    {
        get { return _jellyBeans[_current] as JellyBean; }
    }

    // Gets whether iteration is complete
    public bool IsDone
    {
        get { return _current >= _jellyBeans.Count; }
    }
}

客户端调用:

static void Main(string[] args)
{
    // Build a collection of jelly beans
    JellyBeanCollection collection = new JellyBeanCollection();
    collection[0] = new JellyBean("Cherry");
    collection[1] = new JellyBean("Bubble Gum");
    collection[2] = new JellyBean("Root Beer");
    collection[3] = new JellyBean("French Vanilla");
    collection[4] = new JellyBean("Licorice");
    collection[5] = new JellyBean("Buttered Popcorn");
    collection[6] = new JellyBean("Juicy Pear");
    collection[7] = new JellyBean("Cinnamon");
    collection[8] = new JellyBean("Coconut");

    // Create iterator
    JellyBeanIterator iterator = collection.CreateIterator();

    Console.WriteLine("Gimme all the jelly beans!");

    for (JellyBean item = iterator.First();
        !iterator.IsDone; item = iterator.Next())
    {
        Console.WriteLine(item.Flavor);
    }

    Console.ReadKey();
}

总结

迭代器模式提供了一种方式,我们可以在不暴露集合本身的情况下访问和操作集合中的对象。如果你使用了LINQ,那么你已经使用了该模式。

源代码

https://github.com/exceptionnotfound/DesignPatterns/tree/master/Iterator

原文

https://www.exceptionnotfound.net/the-daily-design-pattern-iterator/

原文地址:https://www.cnblogs.com/talentzemin/p/9907840.html