zoukankan      html  css  js  c++  java
  • 设计模式》迭代器模式

    例子

        public interface ITerator<TSource>
        {
            TSource Current { get; }
            bool HasNext();
            void MoveNext();
        }
    
        public class ArrayIterator<TSource> : ITerator<TSource>
        {
            private TSource[] Sources;
    
            public ArrayIterator(TSource[] sources)
            {
                this.Sources = sources;
                this.index = 0;
            }
    
            private uint index;
            private TSource source;
    
            public TSource Current
            {
                get { return source; }
            }
    
            public bool HasNext()
            {
                return this.index < this.Sources.Length;
            }
    
            public void MoveNext()
            {
                this.source = this.Sources[index];
                this.index++;
            }
        }
    
    
        public interface IAggregate<TSource>
        {
            ITerator<TSource> GetIterator();
        }
    
    
        public class ArrayAggregate<TSource> : IAggregate<TSource>
        {
            private TSource[] Sources;
    
            public ArrayAggregate(TSource[] sources)
            {
                this.Sources = sources;
            }
    
            public ITerator<TSource> GetIterator()
            {
                return new ArrayIterator<TSource>(Sources);
            }
        }
    
            private static void CoreTest()
            {
                ArrayAggregate<string> arrayAggregate = new ArrayAggregate<string>(new string[] {"cxl", "张三", "李四", "王五"});
    
                var iterator = arrayAggregate.GetIterator();
                while (iterator.HasNext())
                {
                    iterator.MoveNext();
                    Console.WriteLine(iterator.Current + "排队买好了蛋炒饭");
                }
            }
    
    

    排队买蛋炒饭

  • 相关阅读:
    每天一个topic----route 设置
    每周一个topic IGMP -- 2013.08.13
    HTTP API 加签名规则
    gitbook 编辑手册
    phpMyAdmin 配置文档
    扒房源线索消息推送设计
    jetty 必知必会
    回溯法解决八皇后问题
    求m行n列个方格的正方形总数
    IOC和AOP的简单实现
  • 原文地址:https://www.cnblogs.com/icxldd/p/15803882.html
Copyright © 2011-2022 走看看