CSharp: Iterator Pattern in donet core 6

资源都来源于互联网

    /// <summary>
    /// 迭代器模式  Iterator Pattern
    /// </summary>
    public interface IIterableCollection
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        IIterator CreateIterator();
    }

  /// <summary>
    /// 迭代器模式  Iterator Pattern
    /// </summary>
    public class FestivalCollection : IIterableCollection
    {

        /// <summary>
        /// 
        /// </summary>
        private readonly List<MusicFestival> _festivals = new();
        /// <summary>
        /// 
        /// </summary>
        public int Count => _festivals.Count;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="festival"></param>
        public void Add(MusicFestival festival) => _festivals.Add(festival);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public MusicFestival Get(int index) => _festivals[index];
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IIterator CreateIterator() => new FestivalIterator(this);


    }

    /// <summary>
    ///  迭代器模式  Iterator Pattern
    /// </summary>
    public interface IIterator
    {
        /// <summary>
        /// 
        /// </summary>
        int Step { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        MusicFestival First();
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        MusicFestival Current();
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        bool MoveNext();
        /// <summary>
        /// 
        /// </summary>
        void Reset();

    }

 /// <summary>
    ///  迭代器模式  Iterator Pattern
    /// </summary>
    public class FestivalIterator : IIterator
    {

        /// <summary>
        /// /
        /// </summary>
        private readonly FestivalCollection _collection;
        /// <summary>
        /// 
        /// </summary>
        private int _position;
        /// <summary>
        /// /
        /// </summary>
        private int _step;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="collection"></param>
        public FestivalIterator(FestivalCollection collection)
        {
            _collection = collection;
            _position = -1;
            _step = 1;
        }

        // It's probably more logical to have a method for changing the step.
        // However, for demo purposes the idea was to show that iterator interface can contain properties too.
        /// <summary>
        /// 
        /// </summary>
        public int Step
        {
            get => _step;
            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), "步长只能是正值。当前迭代器不支持倒退.");
                }

                _step = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>

        public MusicFestival First()
        {
            _position = 0;
            return Current();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public MusicFestival Current() => _collection.Get(_position);
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            var updatedPosition = _position + _step;

            if (updatedPosition < _collection.Count)
            {
                _position = updatedPosition;
                return true;
            }

            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        public void Reset() => _position = -1;
    }

  /// <summary>
    /// 
    /// </summary>
    public class MusicFestival
    {

        /// <summary>
        ///  迭代器模式  Iterator Pattern
        /// </summary>
        /// <param name="name"></param>
        /// <param name="country"></param>
        public MusicFestival(string name, string country)
        {
            Name = name;
            Country = country;
        }
        /// <summary>
        /// 
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Country { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return $"姓名:{this.Name} 国家: {this.Country}";
        }
    }


  /// <summary>
    /// 迭代器模式  Iterator Pattern
    /// </summary>
    public static class MusicFestivalsExecutor
    {

        /// <summary>
        ///  迭代器模式  Iterator Pattern
        /// </summary>
        public static void Execute()
        {
            ConsoleExtension.WriteSeparator(" 迭代器模式  Iterator Pattern:Music festivals demo");

            var collection = new FestivalCollection();

            collection.Add(new MusicFestival("春", "中国"));
            collection.Add(new MusicFestival("四季之歌", "比利时"));
            collection.Add(new MusicFestival("春", "英国"));
            collection.Add(new MusicFestival("夏", "美国"));
            collection.Add(new MusicFestival("冬", "中国"));

            var iterator = collection.CreateIterator();

            Console.WriteLine("初始遍历.");
            Traverse(iterator);

            iterator.Reset();
            iterator.Step = 2;

            Console.WriteLine("\nT应跳过所有其他元素的第二个遍历.");
            Traverse(iterator);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="iterator"></param>
        private static void Traverse(IIterator iterator)
        {
            while (iterator.MoveNext())
            {
                var festival = iterator.Current();
                Console.WriteLine($"音乐节 {festival.Name} 发生在{festival.Country}.");
            }
        }
    }

  

调用:

 Geovin.Du.DuIterator.MusicFestivals.MusicFestivalsExecutor.Execute();

  

输出:

 迭代器模式  Iterator Pattern:Music festivals demo
--------------------------------------------------
初始遍历.
音乐节 春 发生在中国.
音乐节 四季之歌 发生在比利时.
音乐节 春 发生在英国.
音乐节 夏 发生在美国.
音乐节 冬 发生在中国.

T应跳过所有其他元素的第二个遍历.
音乐节 四季之歌 发生在比利时.
音乐节 夏 发生在美国.

  

posted @ 2022-12-18 11:47  ®Geovin Du Dream Park™  阅读(14)  评论(0编辑  收藏  举报