设计模式——迭代模式

Iterator
结构 Interpreter.gif
意图提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。
适用性
  • 访问一个聚合对象的内容而无需暴露它的内部表示。
  • 支持对聚合对象的多种遍历。
  • 为遍历不同的聚合结构提供一个统一的接口(即, 支持多态迭代)。
Code Example
  1 None.gif //  Iterator
  2 None.gif
  3 None.gif //  Intent: "Provide a way to access the elements of an aggregate object 
  4 None.gif //  sequentially without exposing its underlying representation". 
  5 None.gif
  6 None.gif //  For further information, read "Design Patterns", p257, Gamma et al.,
  7 None.gif //  Addison-Wesley, ISBN:0-201-63361-2
  8 None.gif
  9 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Notes:
 10InBlock.gif * Here wish wish to separate node traversal from the nodes themselves. 
 11InBlock.gif * STL in ISO C++ is a highly successful application of this pattern.
 12InBlock.gif * Generic programming is a great way to implement iterators. As this is 
 13InBlock.gif * not yet in C#, we use inheritance. 
 14InBlock.gif *  
 15ExpandedBlockEnd.gif */

 16 None.gif 
 17 None.gif namespace  Iterator_DesignPattern
 18 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 19InBlock.gif    using System;
 20InBlock.gif    using System.Collections;
 21InBlock.gif
 22InBlock.gif    class Node 
 23ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 24InBlock.gif        private string name;
 25InBlock.gif        public string Name 
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 27InBlock.gif            get 
 28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 29InBlock.gif                return name;    
 30ExpandedSubBlockEnd.gif            }

 31ExpandedSubBlockEnd.gif        }

 32InBlock.gif        public Node(string s)
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 34InBlock.gif            name = s;
 35ExpandedSubBlockEnd.gif        }

 36ExpandedSubBlockEnd.gif    }

 37InBlock.gif    
 38InBlock.gif    class NodeCollection 
 39ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 40InBlock.gif        private ArrayList list = new ArrayList();
 41InBlock.gif        private int nodeMax = 0;
 42InBlock.gif        
 43InBlock.gif        // left as a student exercise - implement collection
 44InBlock.gif        // functions to remove and edit entries also
 45InBlock.gif        public void AddNode(Node n)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            list.Add(n); 
 48InBlock.gif            nodeMax++;            
 49ExpandedSubBlockEnd.gif        }
        
 50InBlock.gif        public Node GetNode(int i)
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 52InBlock.gif            return ((Node) list[i]);
 53ExpandedSubBlockEnd.gif        }

 54InBlock.gif
 55InBlock.gif        public int NodeMax 
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{            
 57InBlock.gif            get 
 58ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 59InBlock.gif                return nodeMax;
 60ExpandedSubBlockEnd.gif            }

 61ExpandedSubBlockEnd.gif        }

 62ExpandedSubBlockEnd.gif    }

 63InBlock.gif
 64ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//*
 65InBlock.gif     * The iterator needs to understand how to traverse the collection 
 66InBlock.gif     * It can do that as way it pleases - forward, reverse, depth-first, 
 67ExpandedSubBlockEnd.gif     */

 68InBlock.gif    abstract class Iterator 
 69ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 70InBlock.gif        abstract public Node Next();        
 71ExpandedSubBlockEnd.gif    }

 72InBlock.gif
 73InBlock.gif    class ReverseIterator : Iterator
 74ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 75InBlock.gif        private NodeCollection nodeCollection;
 76InBlock.gif        private int currentIndex;
 77InBlock.gif
 78InBlock.gif        public ReverseIterator (NodeCollection c)
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 80InBlock.gif            nodeCollection = c;            
 81InBlock.gif            currentIndex = c.NodeMax -1// array index starts at 0!
 82ExpandedSubBlockEnd.gif        }

 83InBlock.gif
 84InBlock.gif        // note: as the code stands, if the collection changes,
 85InBlock.gif        // the iterator needs to be restarted 
 86InBlock.gif        override public Node Next()
 87ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 88InBlock.gif            if (currentIndex == -1)
 89InBlock.gif                return null;
 90InBlock.gif            else 
 91InBlock.gif                return(nodeCollection.GetNode(currentIndex--));
 92ExpandedSubBlockEnd.gif        }

 93ExpandedSubBlockEnd.gif    }

 94InBlock.gif    
 95ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 96InBlock.gif    ///    Summary description for Client.
 97ExpandedSubBlockEnd.gif    /// </summary>

 98InBlock.gif    public class Client
 99ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
100InBlock.gif        public static int Main(string[] args)
101ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{   
102InBlock.gif            NodeCollection c = new NodeCollection();
103InBlock.gif            c.AddNode(new Node("first"));
104InBlock.gif            c.AddNode(new Node("second"));
105InBlock.gif            c.AddNode(new Node("third"));
106InBlock.gif
107InBlock.gif            // now use iterator to traverse this
108InBlock.gif            ReverseIterator i = new ReverseIterator(c);
109InBlock.gif
110InBlock.gif            // the code below will work with any iterator type
111InBlock.gif            Node n;
112InBlock.gif            do 
113ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
114InBlock.gif                n = i.Next();
115InBlock.gif                if (n != null
116InBlock.gif                    Console.WriteLine("{0}", n.Name);
117ExpandedSubBlockEnd.gif            }
 while (n != null);
118InBlock.gif                
119InBlock.gif            return 0;
120ExpandedSubBlockEnd.gif        }

121ExpandedSubBlockEnd.gif    }

122ExpandedBlockEnd.gif}

123 None.gif
124 None.gif

转载于:https://www.cnblogs.com/DarkAngel/archive/2005/08/08/209650.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值