迭代器模式的一种应用场景以及C#对于迭代器的内置支持

迭代器模式

先放上gof中对于迭代器模式的介绍镇楼面试

  1. 意图
    提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。
  2. 别名
    游标(Cursor)。
  3. 动机
    一个聚合对象, 如列表(list), 应该提供一种方法来让别人能够访问它的元素,而又不需暴露它的内部结构. 此外,针对不一样的须要,可能要以不一样的方式遍历这个列表。可是即便能够预见到所需的那些遍历操做,你可能也不但愿列表的接口中充斥着各类不一样遍历的操做。有时还可能须要在同一个表列上同时进行多个遍历。迭代器模式均可帮你解决全部这些问题。这一模式的关键思想是将对列表的访问和遍历从列表对象中分离出来并放入一个迭代器(iterator)对象中。迭代器类定义了一个访问该列表元素的接口。迭代器对象负责跟踪当前的元素; 即, 它知道哪些元素已经遍历过了。

类图以下sql

6941baebjw1el54eldhl4j20al07v0t2

工做中遇到的问题

在平常工做中,咱们组负责的系统会常常与外部系统进行大量数据交互,大量数据交互的载体是纯文本文件,咱们须要解析文件每一行的数据,处理后入库,因此在咱们系统中就有了以下的代码了。数据库

        public void ParseFile(string filePath, Encoding fileEncoding)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                using (var sr = new StreamReader(fs, fileEncoding))
                {
                    fs = null;
                    string line = null;
                    while ( (line = sr.ReadLine()) != null )
                    {
                        //解析改行数据
                    }
                }
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }

这样子的代码存在两个问题:1-没法进行单元测试 2-没法扩展。c#

来析一下问题的根源

实际上这两个问题的根源都是由于直接依赖了文件系统。在咱们的业务处理逻辑中,咱们实际关心的是内容,而不是内容从何而来。若是内容格式不发生更改,业务逻辑代码就应该保持不变。文件做为内容的载体,可能会变为socket或者nosql数据库,若是这种状况一旦发生,难道把业务代码copy一份出来,而后把从文件读取数据改成从socket或者nosql读取?在进行单元测试时,我但愿能够提供一个字符串数组就能对个人业务逻辑进行测试,而不是要提供一个文件。那么好了,咱们要作的事情是将具体的数据来源隐藏掉,给业务代码提供一组API,让业务代码使用这组API能够获取到它所关心的内容。换句话说,我要提供一种方法来让人访问数据载体的元素,可是我并不像把数据载体暴露出来,这个目的简直跟迭代器模式的动机一毛同样呀。数组

开始动手改造

在文件解析场景中,文件就是迭代器模式中提到的聚合对象,文件中的每一行就是聚合对象的内部元素。这样咱们先定义出迭代器接口和具体的文件迭代器nosql

  1 public interface IIterator
  2     {
  3         void First();
  4         void Next();
  5         bool IsDone();
  6         string GetCurrentItem();
  7     }
  1 class FileIterator : IIterator
  2     {
  3         private readonly StreamReader _reader = null;
  4         private string _current = null;
  5         public FileIterator(string filePath, Encoding encoding)
  6         {
  7             _reader = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read), encoding);
  8         }
  9 
 10         public void First()
 11         {
 12             Next();
 13         }
 14 
 15         public void Next()
 16         {
 17             _current = _reader.ReadToEnd();
 18         }
 19 
 20         public bool IsDone()
 21         {
 22             return _current == null;
 23         }
 24 
 25         public string GetCurrentItem()
 26         {
 27             return _current;
 28         }
 29     }

而此时咱们的业务代码变成了这样socket

  1 public void ParseFile(IIterator iterator)
  2         {
  3             for (iterator.First(); !iterator.IsDone(); iterator.Next())
  4             {
  5                 var current = iterator.GetCurrentItem();
  6                 Console.WriteLine(current);
  7                 //对数据进行处理
  8             }
  9         }

经过迭代器模式,业务代码对数据载体一无所知,按照给定的一组API,获取想要的数据便可,当进行单元测试时,咱们能够提供一个基于数组的迭代器,对业务代码进行UT函数

class ArrayIterator:IIterator
    {
        private int _currentIndex = -1;
        private readonly string[] _array = null;

        public ArrayIterator(string[] array)
        {
            _array = array;
        }

        public void First()
        {
            Next();
        }

        public void Next()
        {
            _currentIndex++;
        }

        public bool IsDone()
        {
            return _currentIndex >= _array.Length;
        }

        public string GetCurrentItem()
        {
            return _array[_currentIndex];
        }
    }

问题并未彻底解决

细心的读者已经发现了,在我上面实现的文件迭代器是存在问题的,由于我在构造函数里打开了文件流,可是并无关闭它,因此按照C#里的标准作法,文件迭代器要实现 IDisposable接口,咱们还要实现一个标准的Dispose模式,咱们的文件迭代器就变成了这样。单元测试

  1 class FileIterator : IIterator,IDisposable
  2     {
  3         private StreamReader _reader = null;
  4         private string _current = null;
  5         private bool _disposed = false;
  6         private FileStream _fileStream = null;
  7         private readonly string _filePath = null;
  8         private readonly Encoding _encoding = null;
  9         public FileIterator(string filePath, Encoding encoding)
 10         {
 11             _filePath = filePath;
 12             _encoding = encoding;
 13         }
 14 
 15         public void First()
 16         {
 17             //原先在构造函数里实例化StreamReader不太合适,转移到First方法里
 18             _fileStream = new FileStream(_filePath, FileMode.Open, FileAccess.Read);
 19             _reader = new StreamReader(_fileStream, _encoding);
 20             _fileStream = null;
 21             Next();
 22         }
 23 
 24         public void Next()
 25         {
 26             _current = _reader.ReadToEnd();
 27         }
 28 
 29         public bool IsDone()
 30         {
 31             return _current == null;
 32         }
 33 
 34         public string GetCurrentItem()
 35         {
 36             return _current;
 37         }
 38 
 39         public void Dispose()
 40         {
 41             Dispose(true);
 42             GC.SuppressFinalize(this);
 43         }
 44 
 45         protected virtual void Dispose(bool disposing)
 46         {
 47             if (_disposed)
 48             {
 49                 return;
 50             }
 51             if (disposing)
 52             {
 53                 if (_reader != null)
 54                 {
 55                     _reader.Dispose();
 56                 }
 57                 if (_fileStream != null)
 58                 {
 59                     _fileStream.Dispose();
 60                 }
 61             }
 62             _disposed = true;
 63         }
 64 
 65         ~FileIterator()
 66         {
 67             Dispose(false);
 68         }
 69     }

配合此次改造,业务代码也要作一些改变测试

  1 public void ParseFile(IIterator iterator)
  2         {
  3             try
  4             {
  5                 for (iterator.First(); !iterator.IsDone(); iterator.Next())
  6                 {
  7                     var current = iterator.GetCurrentItem();
  8                     Console.WriteLine(current);
  9                     //对数据进行处理
 10                 }
 11             }
 12             finally
 13             {
 14                 var disposable = iterator as IDisposable;
 15                 if (disposable != null)
 16                 {
 17                     disposable.Dispose();
 18                 }
 19             }
 20         }

使用迭代器模式,成功解耦了对文件系统的依赖,咱们能够为所欲为地进行单元测试,数据载体的变更再也影响不到业务代码。

C#早就看穿了一切

上面的章节,我实现了经典gof迭代器模式,实际上,迭代器模式的应用是如此的广泛,以致于有些语言已经提供了内置支持,在C#中,与迭代器有关的有foreach关键字,IEnumerable,IEnumerable<T>,IEnumerator,IEnumerator<T>四个接口,看起来有四个接口,其实是2个,只是由于在 C#2.0版本以前未提供泛型支持,在这里仅对两个泛型接口进行讨论。

在C#中,接口IEnumerator<T>就是迭代器,对应上面的Iterator,而IEnumerable<T>接口就是聚合对象,对应上面的Aggregate。在IEnumerable<T>中只定义了一个方法

public Interface IEnumerable<T>
{
    IEnumerator<T> GetEnumerator();
}

而foreach关键字c#专门为了遍历迭代器才出现的,我面试别人的时候,特别喜欢问这样一个问题:“知足什么条件的类型实例才能够被foreach遍历?"看起来正确答案应该是实现了IEnumerable<T>接口的类型,实际上C#并不要求类型实现IEnumerable<T>接口,只要类型中定义了public IEnumerator<T> GetEnumerator()接口便可。

对于IEnumerator<T>接口,微软已经想到了迭代器中可能会用到非托管对象(实际上微软刚开始忽略了这个事情,因此最初的非泛型接口IEnumerator并无继承IDisposable接口,直到2.0后才让泛型接口IEnumerator<T>继承了IDisposable),因此它的定义是这样子的。

public interface IEnumerator<out T> : IDisposable, IEnumerator
{    
        new T Current {get;}
}

public interface IEnumerator
{
    bool MoveNext();
      
    Object Current {get;}
      
    void Reset();
}

在C#的IEnumerator<T>中,实际上将gof经典设计中的First(),IsDone()和Next()三个方法全都合并到了MoveNext()方法中,第一次迭代前现调用MoveNext(),并经过返回值判断迭代是否结束,还额外提供了一个Reset方法来重置迭代器。当咱们使用foreach写出遍历一个对象的代码时,编译器会将咱们的代码进行转换。好比咱们如今要遍历一个32位整型List

List<int> list = new List<int> {0,1,2,3,4};
foreach (var item in list)
{
   Console.WriteLine(item);
}

编译时编译器会将代码变成相似下面这样

List<int> list = new List<int> {0,1,2,3,4};
using (var enumerator = list.GetEnumerator())
{
        while (enumerator.MoveNext())
        {
            Console.WriteLine(enumerator.Current);
        }
}

继续改造咱们的代码

既然C#中已经内置了迭代器接口,咱们就没有必要定义本身的IIterator接口了,直接使用IEnumerable<T>和IEnumerator<T>接口便可。

 class FileEnumerable : IEnumerable<string>
    {
        private readonly string _filePath;
        private readonly Encoding _fileEncoding;
        public FileEnumerable(string filePath, Encoding fileEncoding)
        {
            _filePath = filePath;
            _fileEncoding = fileEncoding;
        }
        public IEnumerator<string> GetEnumerator()
        {
            return new FileEnumerator(_filePath,_fileEncoding);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }

  

public class FileEnumerator : IEnumerator<string>
    {
        private string _current;
        private FileStream _fileStream;
        private StreamReader _reader;
        private readonly string _filePath;
        private readonly Encoding _fileEncoding;
        private bool _disposed = false;
        private bool _isFirstTime = true;
        public FileEnumerator(string filePath, Encoding fileEncoding)
        {
            _filePath = filePath;
            _fileEncoding = fileEncoding;
        }
        public string Current
        {
            get { return _current; }
        }

        object IEnumerator.Current
        {
            get { return Current; }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }
            if (disposing)
            {
                if (_reader != null)
                {
                    _reader.Dispose();
                }
                if (_fileStream != null)
                {
                    _fileStream.Dispose();
                }
            }
            _disposed = true;
        }

        public bool MoveNext()
        {
            if (_isFirstTime)
            {
                _fileStream = new FileStream(_filePath, FileMode.Open, FileAccess.Read);
                _reader = new StreamReader(_fileStream, _fileEncoding);
                _fileStream = null;
                _isFirstTime = false;
            }
            return (_current = _reader.ReadLine()) != null;
        }

        public void Reset()
        {
            throw new NotImplementedException();
        }

        ~FileEnumerator()
        {
            Dispose(false);
        }
    }

  而此时咱们的业务代码变成了这样子

public void ParseFile(IEnumerable<string> aggregate)
        {
            foreach (var item in aggregate)
            {
                Console.WriteLine(item);
                // //对数据进行处理
            }
        }

  在进行单元测试时,我能够直接传递一个字符串数组进去了。

最终版本

看起来咱们对于代码的重构已经完美了,可是实际上C#对于迭代器的内置支持要更完全,在上面,咱们必需要本身写一个实现了IEnumerator<T>接口的类型,这个工做虽然不难,可是仍是有点繁琐的,C# 针对迭代器模式,提供了yield return和yield break来帮助咱们更快更好的实现迭代器模式。下面是代码重构的最终版本,咱们无需本身定义FileEnumerator类了

 class FileEnumerable : IEnumerable<string>
    {
        private readonly string _filePath;
        private readonly Encoding _fileEncoding;
        public FileEnumerable(string filePath, Encoding fileEncoding)
        {
            _filePath = filePath;
            _fileEncoding = fileEncoding;
        }
        public IEnumerator<string> GetEnumerator()
        {
            FileStream fileStream = null;
            try
            {
                fileStream = new FileStream(_filePath, FileMode.Open, FileAccess.Read);
                using (var reader = new StreamReader(fileStream, _fileEncoding))
                {
                    fileStream = null;
                    string line = null;
                    while ((line = reader.ReadLine()) != null)
                    {
                        yield return line;
                    }
                    yield break;
                }
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Dispose();
                }
            }

        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }

  这里编译器会根据咱们的代码,结合yield return和yield break来帮助咱们生存一个实现了IEnumerator<string>接口的类型出来。

关于Dispose模式,和yield return,yield break本篇不作过多展开,有兴趣的能够找下资料,msdn会告诉你

相关文章
相关标签/搜索