• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            模式設計c#--行為型--iterator

            名稱 Iterator
            結構 o_iterator.bmp
            意圖 提供一種方法順序訪問一個聚合對象中各個元素, 而又不需暴露該對象的內部表示。
            適用性
            • 訪問一個聚合對象的內容而無需暴露它的內部表示。
            • 支持對聚合對象的多種遍歷。
            • 為遍歷不同的聚合結構提供一個統一的接口(即, 支持多態迭代)。


            namespace Iterator_DesignPattern
            {
                
            using System;
                
            using System.Collections;

                
            class Node 
                
            {
                    
            private string name;
                    
            public string Name 
                    
            {
                        
            get 
                        
            {
                            
            return name;    
                        }

                    }

                    
            public Node(string s)
                    
            {
                        name 
            = s;
                    }

                }

                
                
            class NodeCollection 
                
            {
                    
            private ArrayList list = new ArrayList();
                    
            private int nodeMax = 0;
                    
                    
            // left as a student exercise - implement collection
                    
            // functions to remove and edit entries also
                    public void AddNode(Node n)
                    
            {
                        list.Add(n); 
                        nodeMax
            ++;            
                    }
                    
                    
            public Node GetNode(int i)
                    
            {
                        
            return ((Node) list[i]);
                    }


                    
            public int NodeMax 
                    
            {            
                        
            get 
                        
            {
                            
            return nodeMax;
                        }

                    }

                }


                
            /*
                 * The iterator needs to understand how to traverse the collection 
                 * It can do that as way it pleases - forward, reverse, depth-first, 
                 
            */

                
            abstract class Iterator 
                
            {
                    
            abstract public Node Next();        
                }


                
            class ReverseIterator : Iterator
                
            {
                    
            private NodeCollection nodeCollection;
                    
            private int currentIndex;

                    
            public ReverseIterator (NodeCollection c)
                    
            {
                        nodeCollection 
            = c;            
                        currentIndex 
            = c.NodeMax -1// array index starts at 0!
                    }


                    
            // note: as the code stands, if the collection changes,
                    
            // the iterator needs to be restarted 
                    override public Node Next()
                    
            {
                        
            if (currentIndex == -1)
                            
            return null;
                        
            else 
                            
            return(nodeCollection.GetNode(currentIndex--));
                    }

                }

                
                
            /// <summary>
                
            ///    Summary description for Client.
                
            /// </summary>

                public class Client
                
            {
                    
            public static int Main(string[] args)
                    
            {   
                        NodeCollection c 
            = new NodeCollection();
                        c.AddNode(
            new Node("first"));
                        c.AddNode(
            new Node("second"));
                        c.AddNode(
            new Node("third"));

                        
            // now use iterator to traverse this
                        ReverseIterator i = new ReverseIterator(c);

                        
            // the code below will work with any iterator type
                        Node n;
                        
            do 
                        
            {
                            n 
            = i.Next();
                            
            if (n != null
                                Console.WriteLine(
            "{0}", n.Name);
                        }
             while (n != null);
                            
                        
            return 0;
                    }

                }

            }

            posted on 2006-01-03 16:08 夢在天涯 閱讀(918) 評論(1)  編輯 收藏 引用 所屬分類: Design pattern

            評論

            # re: 模式設計c#--行為型--iterator 2006-04-24 18:48 夢在天涯

            比較簡單了,在net里太多的集合類拉,哈哈,就是那2個interface.  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807602
            • 排名 - 5

            最新評論

            閱讀排行榜

            国产精品成人久久久| 精品乱码久久久久久夜夜嗨| 久久青青草原亚洲av无码| 武侠古典久久婷婷狼人伊人| 日本WV一本一道久久香蕉| 久久亚洲国产成人精品性色| 久久福利青草精品资源站免费| 久久99精品免费一区二区| 亚洲精品无码成人片久久| 精品视频久久久久| 久久精品毛片免费观看| 久久午夜综合久久| 精品国产91久久久久久久| 热99RE久久精品这里都是精品免费| 国产91色综合久久免费分享| 久久久午夜精品| 久久亚洲视频| 欧美久久综合性欧美| 久久久久99精品成人片直播| 一本久久综合亚洲鲁鲁五月天| 91久久精一区二区三区大全| 一本大道久久香蕉成人网| 久久精品一区二区国产| 色欲久久久天天天综合网| 久久成人永久免费播放| 天堂久久天堂AV色综合| 久久无码国产专区精品| 久久久国产亚洲精品| 亚洲精品tv久久久久久久久久| 大蕉久久伊人中文字幕| 伊人久久大香线蕉精品| 99久久国语露脸精品国产| 久久精品aⅴ无码中文字字幕重口| 久久婷婷色综合一区二区| 久久只有这里有精品4| 久久无码高潮喷水| 欧洲成人午夜精品无码区久久| 久久久精品2019免费观看| 狠狠色丁香久久婷婷综合五月| 77777亚洲午夜久久多喷| 精品国产91久久久久久久|