设计模式 迭代模式(Iterator Pattern)

概述

迭代器(Iterator)模式,又叫做游标(Cursor)模式。GOF给出的定义为:提供一种方法访问一个容器(container)对象中各个元素,而又不需暴露该对象的内部细节。 从定义可见,迭代器模式是为容器而生。很明显,对容器对象的访问必然涉及到遍历算法。你可以一股脑的将遍历方法塞到容器对象中去;或者根本不去提供什么遍历算法,让使用容器的人自己去实现去吧。这两种情况好像都能够解决问题。

意图

提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。

适用性

1.访问一个聚合对象的内容而无需暴露它的内部表示。

2.支持对聚合对象的多种遍历。

3.为遍历不同的聚合结构提供一个统一的接口(即, 支持多态迭代)。 

结构图

实例

  1 // Iterator
  2 
  3 /* Notes:
  4  * Here wish wish to separate node traversal from the nodes themselves. 
  5  * STL in ISO C++ is a highly successful application of this pattern.
  6  * Generic programming is a great way to implement iterators. As this is 
  7  * not yet in C#, we use inheritance. 
  8  *  
  9  */
 10  
 11 namespace Iterator_DesignPattern
 12 {
 13     using System;
 14     using System.Collections;
 15 
 16     class Node 
 17     {
 18         private string name;
 19         public string Name 
 20         {
 21             get 
 22             {
 23                 return name;    
 24             }
 25         }
 26         public Node(string s)
 27         {
 28             name = s;
 29         }
 30     }
 31     
 32     class NodeCollection 
 33     {
 34         private ArrayList list = new ArrayList();
 35         private int nodeMax = 0;
 36         
 37         // left as a student exercise - implement collection
 38         // functions to remove and edit entries also
 39         public void AddNode(Node n)
 40         {
 41             list.Add(n); 
 42             nodeMax++;            
 43         }        
 44         public Node GetNode(int i)
 45         {
 46             return ((Node) list[i]);
 47         }
 48 
 49         public int NodeMax 
 50         {            
 51             get 
 52             {
 53                 return nodeMax;
 54             }
 55         }
 56     }
 57 
 58     /*
 59      * The iterator needs to understand how to traverse the collection 
 60      * It can do that as way it pleases - forward, reverse, depth-first, 
 61      */
 62     abstract class Iterator 
 63     {
 64         abstract public Node Next();        
 65     }
 66 
 67     class ReverseIterator : Iterator
 68     {
 69         private NodeCollection nodeCollection;
 70         private int currentIndex;
 71 
 72         public ReverseIterator (NodeCollection c)
 73         {
 74             nodeCollection = c;            
 75             currentIndex = c.NodeMax -1; // array index starts at 0!
 76         }
 77 
 78         // note: as the code stands, if the collection changes,
 79         // the iterator needs to be restarted 
 80         override public Node Next()
 81         {
 82             if (currentIndex == -1)
 83                 return null;
 84             else 
 85                 return(nodeCollection.GetNode(currentIndex--));
 86         }
 87     }
 88     
 89     /// <summary>
 90     ///    Summary description for Client.
 91     /// </summary>
 92     public class Client
 93     {
 94         public static int Main(string[] args)
 95         {   
 96             NodeCollection c = new NodeCollection();
 97             c.AddNode(new Node("first"));
 98             c.AddNode(new Node("second"));
 99             c.AddNode(new Node("third"));
100 
101             // now use iterator to traverse this
102             ReverseIterator i = new ReverseIterator(c);
103 
104             // the code below will work with any iterator type
105             Node n;
106             do 
107             {
108                 n = i.Next();
109                 if (n != null) 
110                     Console.WriteLine("{0}", n.Name);
111             } while (n != null);
112                 
113             return 0;
114         }
115     }
116 }

 

 

人生如棋、我愿为卒、行动虽缓、从未退过

原文地址:https://www.cnblogs.com/sunjinpeng/p/2444424.html