Patterns in SOME –Iterator

 
Code in C#:
 
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;
         }
     }
}
 
Code in SOME:
  
CNode
       (string r_Name)                   //constructor and a read-only property
 
CNodeCollection ->CNode[][_list]            //collection referrence define
       int r_NodeMax
       AddNode(CNode)
       GetNode(int)
 
AIterator
       CNode a_Next()
      
CReverseIterator : AIterator ->CNodeCollection[_nodeCollection]
       int _currentIndex
       (CNodeCollection)
       CNode o_Next()
 
CClient
       main
 
 
CClient.main
{
       CNodeCollection c.();
       CNode node1.("first");
      
       c.AddNode(node[node1])
       {
              <%
              _list.Add(node);
               nodeMax++;
              %>
       };
      
       CNode node2.("second");
       c.AddNode(node2);                            //need not re-define
      
       CNode node3.("third"); //need not re-define
       c.AddNode(node3);
 
       // now use iterator to traverse this
       CReverseIterator i.(_nodeCollection = c)
       {
              <% _currentIndex = c.NodeMax -1; /*array index starts at 0!*/ %>
       };
 
       // the code below will work with any iterator type
       CNode n;
       <%
       do
       {
       %>
              n = i.Next<CReverseIterator>()
              {
                     <%
                  if (_currentIndex == -1)
                  {
                                   return null;
                            }
                            else
                            {
                                   return(_nodeCollection.GetNode(i[_currentIndex--]))
                                   {
                                          return ((CNode) list[i]);
                                   };
                            }
                     %>
              };
       <%
              if (n != null)
              {
                     Console.WriteLine("{0}", n.Name);
              }
       } while (n != null);
                           
 return 0;
 %>
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值