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 梦在天涯 阅读(900) 评论(1)  编辑 收藏 引用 所属分类: Design pattern

评论

# re: 模式设计c#--行为型--iterator 2006-04-24 18:48 梦在天涯

比较简单了,在net里太多的集合类拉,哈哈,就是那2个interface.  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

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

搜索

  •  

积分与排名

  • 积分 - 1785188
  • 排名 - 5

最新评论

阅读排行榜