• <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++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {C#基礎(chǔ)}

            模式設(shè)計c#--行為型--iterator

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


            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: 模式設(shè)計c#--行為型--iterator 2006-04-24 18:48 夢在天涯

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

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計

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

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1808012
            • 排名 - 5

            最新評論

            閱讀排行榜

            日韩十八禁一区二区久久| 性高湖久久久久久久久| 国产午夜电影久久| 久久久久亚洲av毛片大| 亚洲中文字幕伊人久久无码 | 亚洲第一永久AV网站久久精品男人的天堂AV | 久久综合色老色| 精品久久久久久亚洲精品 | 久久精品嫩草影院| 一个色综合久久| 成人亚洲欧美久久久久| 伊人久久精品无码av一区| 久久精品二区| 97久久精品午夜一区二区| 国产精品久久久久久久app| 国产69精品久久久久99| 亚洲AV无码久久寂寞少妇| 久久综合色之久久综合| 国产精品无码久久久久久| 久久精品国产亚洲AV不卡| 久久精品国产精品亚洲人人| 久久99毛片免费观看不卡| 新狼窝色AV性久久久久久| 久久国产AVJUST麻豆| 久久人人爽人人爽AV片| 国产成人综合久久精品尤物| 久久国产精品-久久精品| 国内精品久久久人妻中文字幕| 国产精品久久新婚兰兰| 亚洲欧美一区二区三区久久| 日韩亚洲国产综合久久久| 久久se这里只有精品| 国产成人久久久精品二区三区 | 欧美精品福利视频一区二区三区久久久精品 | 久久狠狠一本精品综合网| 国产精品99久久久久久猫咪| 99久久免费国产精品| 狠狠色丁香婷婷综合久久来来去| 欧美伊香蕉久久综合类网站| 人人狠狠综合久久亚洲88| 99久久国产主播综合精品|