(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語句只能對集合進行遍歷,不能在遍歷的同時更改集合中的元素

總結

迭代器模式就是抽象一個迭代器類來分離了集合對象的遍歷行爲,這樣既可以做到不暴露集合的內部結構,又可以讓外部代碼透明地訪問集合內部的數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章