zoukankan      html  css  js  c++  java
  • 迭代模式

    代码
    using System;
    using System.Collections;
    using System.Collections.Generic;

    public class Node
    {
        
    public string Name
        {
            
    get;
            
    set;
        }
        
    public Node(string s)
        {
            
    this.Name=s;
        }
    }

    public class NodeCollection
    {
        
    private ArrayList list=new ArrayList();
        
    private int nodeIndex=0;
        
    public void AddNode(Node n)
        {
            list.Add(n);
            nodeIndex
    ++;
        }
        
    public Node GetNode(int i)
        {
            
    return ((Node)list[i]);
        }
        
        
    public int NodeIndex
        {
            
    get
            {
                
    return nodeIndex;
            }
        }
    }

    public abstract class Iterator
    {
        
    public abstract  Node Next();
    }
    public class ReverseIterator:Iterator
    {
        
    private NodeCollection nodeCollection;
        
    private int currentIndex;
        
        
    public ReverseIterator(NodeCollection c)
        {
            
    this.nodeCollection=c;
            currentIndex
    =c.NodeIndex-1;
        }
        
        
    public override Node Next()
        {
            
    if(currentIndex==-1)
            {
                
    return null;
            }
            
    else
            {
                
    return (nodeCollection.GetNode(currentIndex--));
            }
        }
    }


    public class MyClass
    {
        
    public static void Main()
        {
            NodeCollection c
    =new NodeCollection();
            c.AddNode(
    new Node("first"));
            c.AddNode(
    new Node("Second"));
            c.AddNode(
    new Node("third"));
            
            ReverseIterator i
    =new ReverseIterator(c);
            Node n;
            
    do
            {
                n
    =i.Next();
                
    if(n!=null)
                {
                    Console.WriteLine(
    "{0}",n.Name);
                }
            }
            
    while(n!=null);
        }
    }
    名称 Iterator
    结构  
    意图 提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。
    适用性
    • 访问一个聚合对象的内容而无需暴露它的内部表示。
    • 支持对聚合对象的多种遍历。
    • 为遍历不同的聚合结构提供一个统一的接口(即, 支持多态迭代)。
  • 相关阅读:
    小程序前端直传阿里云oss的一些记录
    小程序的两种分页做法(后端返回分页及总页数字段与否)
    小程序模糊搜索(词汇联想)
    小程序自定义组件的两种方式
    js对数据的一些处理方法(待完善)
    小程序关于登录授权回跳页面的两个问题记录
    小程序登录的一些简单步骤
    关于js的方括号[]属性赋值的一些记录
    js状态转化的简单写法
    微信企业号开发node版
  • 原文地址:https://www.cnblogs.com/mikechang/p/1724113.html
Copyright © 2011-2022 走看看