陈巧倩

设计模式-迭代器模式

· 280 words · 2 minutes to read
Categories: CSharp
Tags: Design Pattern

Iterator迭代器模式属于行为型设计模式,为了解决遍历集合使用统一的接口问题,提供了一种在不暴露其内部表示的情况下顺序访问聚合对象元素的方法。

Design Pattern:Iterator 🔗

基本介绍 🔗

构成 🔗

  • Iterator:抽象迭代器类,用来定义访问和遍历序列中元素的操作的接口。
  • ConcreteIterator:具体迭代器类,实现了Iterator接口,完成对序列对象的遍历,同时在对聚合进行遍历时跟踪其当前位置。
  • Aggregate:抽象聚合类,用于定义创建迭代器对象的操作。
  • ConcreteAggregate:具体聚合类,实现了Aggregate相应迭代器的接口。

优点 🔗

  • 迭代器模式访问或者遍历聚合对象的内容时无需暴露聚合对象的内部表示。
  • 迭代器模式为遍历不同的集合结构提供了一个统一的接口,不关心集合结构的算法是什么。

缺点 🔗

  • 迭代器模式不是直接访问集合结构,会带来一些性能上的消耗

实例 🔗

C#中迭代器模式的实例,通过这段时间的创业,我决定去家族旗下的某个集团当个董事长,上任第一件事就是认识一下所有员工。

public class Employee
{
    /// <summary>
    /// 员工名字
    /// </summary>
    public string Name;
    /// <summary>
    /// 员工Id
    /// </summary>
    public int Id;

    public Employee(string name, int id)
    {
        Name = name;
        Id = id;
    }
}
/// <summary>
/// 抽象迭代器
/// </summary>
public interface Iterator
{
    /// <summary>
    /// 首个item
    /// </summary>
    /// <returns></returns>
    Employee FirstItem();
    /// <summary>
    /// 下一个item
    /// </summary>
    /// <returns></returns>
    Employee NextItem();
    /// <summary>
    /// 是否存在下一个item
    /// </summary>
    /// <returns></returns>
    bool HasNext();
    /// <summary>
    /// 当前item
    /// </summary>
    /// <returns></returns>
    Employee CurrentItem();
}
/// <summary>
/// 具体迭代器
/// </summary>
public class ConcreteIterator : Iterator
{
    private readonly ConcreteAggregate _aggregate;
    private int _currentItemIndex = 0;

    public ConcreteIterator(ConcreteAggregate cAggregate)
    {
        _aggregate = cAggregate;
    }

    public Employee FirstItem()
    {
        return _aggregate[0];
    }

    public Employee NextItem()
    {
        Employee returnObj = null;
        if (_currentItemIndex < _aggregate.Count - 1)
        {
            returnObj = _aggregate[++_currentItemIndex];
        }
        return returnObj;
    }

    public Employee CurrentItem()
    {
        return _aggregate[_currentItemIndex];
    }

    public bool HasNext()
    {
        return _currentItemIndex >= _aggregate.Count;
    }
}
/// <summary>
/// 抽象聚合
/// </summary>
public interface Aggregate
{
    /// <summary>
    /// 创建迭代器
    /// </summary>
    /// <returns></returns>
    Iterator Create();
}
/// <summary>
/// 具体聚合
/// </summary>
public class ConcreteAggregate : Aggregate
{
    readonly List<Employee> _employees = new List<Employee>();

    public Iterator Create()
    {
        return new ConcreteIterator(this);
    }

    public int Count => _employees.Count;

    public Employee this[int index]
    {
        get => _employees[index];
        set => _employees.Insert(index, value);
    }
}

实际运行:

ConcreteAggregate aggregate = new ConcreteAggregate();

aggregate[0] = new Employee("dongweiyi",1);
aggregate[1] = new Employee("sunchangpeng",2);

Iterator iterator = aggregate.Create();
Employee currentItem = iterator.FirstItem();
while (currentItem != null)
{
    currentItem = iterator.NextItem();
}

结论 🔗

搬砖愉快!