[学习笔记:设计模式] 16_迭代器模式

定义:提供一种方法来访问聚合对象,而不用暴露这个对象的内部表示。迭代器模式别名为游标(Cursor)


迭代器模式包含如下角色:

Iterator(抽象迭代器):定义了访问和遍历元素的接口,声明了用于遍历数据元素的方法。

ConcreteIterator(具体迭代器):实现了抽象迭代器接口,完成对聚合对象的遍历,同时在具体迭代器中通过游标来记录在聚合对象中所处的当前位置,在具体实现时,游标通常是一个表示位置的非负整数。

Aggregate(抽象聚合类):用于存储和管理元素对象,声明一个createIterator()方法用于创建一个迭代器对象,充当抽象迭代器工厂角色。

ConcreteAggregate(具体聚合类):实现了在抽象聚合类中声明的createIterator()方法,该方法返回一个与该具体聚合类对应的具体迭代器ConcreteIterator实例。


举例
电视遥控器切换频道

- 抽象聚合类

 public interface ITelevision
        {
            //创建迭代器
            ITVIterator CreateIterator();


            //返回元素集合
            Object[] GetChannels();
        }

- 具体聚合类

public class ConcreteTV : ITelevision
        {
            private Object[] TVChannels = { "CCTV_1", "CCTV_2", "CCTV_3", "CCTV_4", "CCTV_5" };

            public ITVIterator CreateIterator()
            {
                return new ConcreteTVIterator(this);
            }

            public Object[] GetChannels()
            {
                return TVChannels;
            }
        }

- 抽象迭代器

 public interface ITVIterator
        {
            void SetChannel(int i);
            void next();
            void Previous();
            bool isLast();
            bool isFirst();
            Object CurrentChannel();
        }

- 具体迭代器

public class ConcreteTVIterator : ITVIterator
        {
            ITelevision television = new ConcreteTV();
            private Object[] channels;  //遍历对象
            private int channelIndex = 0;  //游标

            //构造函数中获取集合对象
            public ConcreteTVIterator(ITelevision television)
            {
                this.television = television;
                channels = television.GetChannels();
            }

            //返回当前位置
            public object CurrentChannel()
            {
                Console.WriteLine(channels[channelIndex]);
                return channels[channelIndex];
            }

            //判断是否为第一个元素
            public bool isFirst()
            {
                return channelIndex == 0;
            }

            //判断是否为最后一个元素
            public bool isLast()
            {
                return channelIndex == channels.Length;
            }

            //获取下一个元素
            public void next()
            {
                if (!isLast())
                {
                    channelIndex++;
                }
                else
                {
                    channelIndex = 0;
                }
            }

            //获取上一个元素
            public void Previous()
            {
                if (!isFirst())
                {
                    channelIndex--;
                }
                else
                {
                    channelIndex = channels.Length;
                }
            }

            //获取指定位置元素
            public void SetChannel(int i)
            {
                if (i >= 0 && i <= channels.Length)
                {
                    channelIndex = i;
                }
            }
        }
    }

- 客户类

class Program
    {
        static void Main(string[] args)
        {
            ITelevision television = new ConcreteTV();
            ITVIterator tvIterator = television.CreateIterator();

            tvIterator.next();
            tvIterator.CurrentChannel();
            tvIterator.SetChannel(3);
            tvIterator.CurrentChannel();
        }          
    }

总结:在抽象聚合类中定义创建迭代器等方法,在具体抽象类中实现这些方法并定义集合对象,具体迭代器也是在实现抽象迭代器定义的方法之前,通过构造函数注入需要遍历的聚合类对象并获取集合,并定义一个或多个游标变量来记录当前位置。

  • 20
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值