简单迭代器

抽象类Iterator

namespace myIterator
{
    public abstract class Iterator
    {
        public abstract bool HasNext();
        public abstract object Current();
    }
}
View Code

抽象集合类

namespace myIterator
{
    public abstract class Aggregate
    {
        public abstract Iterator Iterator();
    }
}
View Code

Iterator实现类

namespace myIterator
{
    public class ConcreteIterator : Iterator
    {
        private readonly ConcreteAggregate _aggregate;
        private int _current;

        public ConcreteIterator(ConcreteAggregate aggregate)
        {
            this._aggregate = aggregate;
        }

        public override bool HasNext()
        {
            return _current < _aggregate.Count ? true : false;
        }

        public override object Current()
        {
            return _aggregate[_current++];
        }
    }
}
View Code

集合实现类

using System;
using System.Runtime;

namespace myIterator
{
    public class ConcreteAggregate : Aggregate
    {
        private readonly object[] _emptyArray = new object[0];
        private object[] _items = new object[4];
        private int now;


        public int Count
        {
            get { return _items.Length; }
        }

        public object this[int index]
        {
            get { return _items[index]; }
            set
            {
                if (now < _items.Length)
                    _items[now++] = value;
                else
                {
                    var size = (int) (_items.Length*1.5);
                    var tem = new object[size];
                    Array.Copy(_items, 0, tem, 0, _items.Length);
                    _items = tem;
                    tem = null;
                }
            }
        }

        public int Capacity
        {
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
            get
            {
                return _items.Length;
            }
            set
            {
                if (value < now)
                    throw new Exception();
                if (value == _items.Length)
                    return;
                if (value > 0)
                {
                    var objArray = new object[value];
                    if (now > 0)
                        Array.Copy(_items, 0, objArray, 0, now);
                    _items = objArray;
                }
                else
                    _items = _emptyArray;
            }
        }

        public override Iterator Iterator()
        {
            return new ConcreteIterator(this);
        }

        public void Add(object item)
        {
            if (now == _items.Length)
                EnsureCapacity(now + 1);
            _items[now++] = item;
        }

        private void EnsureCapacity(int min)
        {
            if (_items.Length >= min)
                return;
            int num = _items.Length == 0 ? 4 : _items.Length*2;
            if (num < min)
                num = min;
            Capacity = num;
        }
    }
}
View Code

测试类

using System;

namespace myIterator
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            T0();
            Console.ReadKey();
        }


        private static void T0()
        {
            var ca = new ConcreteAggregate();
            ca.Add("a");
            ca.Add("b");
            ca.Add("c");
            ca.Add("d");
            ca.Add("e");

            Iterator itor = ca.Iterator();
            while (itor.HasNext())
            {
                Console.WriteLine(itor.Current());
            }
        }
    }
}
View Code

 

posted @ 2013-09-05 13:20  eyotata  阅读(152)  评论(0编辑  收藏  举报