好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

深入理解Java设计模式之迭代器模式

一、什么是迭代器模式

迭代器模式是针对集合对象而生的,对于集合对象而言,肯定会涉及到对集合的添加和删除操作,同时也肯定支持遍历集合元素的操作,我们此时可以把遍历操作放在集合对象中,但这样的话,集合对象既承担太多的责任了,面向对象设计原则中有一条就是单一职责原则,所有我们要尽可能地分离这些职责,用不同的类取承担不同的责任,迭代器模式就是用迭代器类来承担遍历集合的职责。

定义 :迭代器模式提供了一种方法顺序访问一个聚合对象中的各个元素,而又无需暴露该对象的内部实现,这样既可以做到不暴露集合的内部结构,又可让外部代码透明地访问集合内部的数据

二、迭代器模式的结构

抽象容器角色( aggregate ):负责提供创建具体迭代器角色的接口,一般是一个接口,提供一个iterator()方法,例如java中的collection接口,list接口,set接口等。

具体容器角色( concreteaggregate ):就是实现抽象容器的具体实现类,比如list接口的有序列表实现arraylist,list接口的链表实现linkedlist,set接口的哈希列表的实现hashset等。

抽象迭代器角色( iterator ):负责定义访问和遍历元素的接口。

具体迭代器角色( concreteiterator ):实现迭代器接口,并要记录遍历中的当前位置。

三、迭代器模式的使用场景

访问一个集合对象的内容而无需暴露它的内部表示

为遍历不同的集合结构提供一个统一的接口

四、迭代器模式的优缺点

优点:

迭代器模式使得访问一个聚合对象的内容而无需暴露它的内部表示,即迭代抽象。

迭代器模式为遍历不同的集合结构提供了一个统一的接口,从而支持同样的算法在不同的集合结构上进行操作

缺点:

迭代器模式在遍历的同时更改迭代器所在的集合结构会导致出现异常。所以使用foreach语句只能在对集合进行遍历,不能在遍历的同时更改集合中的元素。

五、迭代器模式的实现

抽象聚合类

?

1

2

3

4

public interface ilistcollection

{

     iterator getiterator();

}

迭代器抽象类

?

1

2

3

4

5

6

7

public interface iterator

{

     bool movenext();

     object getcurrent();

     void next();

     void reset();

}

具体聚合类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public class concretelist : ilistcollection

{

     int [] collection;

     public concretelist()

     {

         collection = new int [] { 2 , 4 , 6 , 8 };

     }

     public iterator getiterator()

     {

         return new concreteiterator( this );

     }

     public int length

     {

         get { return collection.length; }

     }

     public int getelement( int index)

     {

         return collection[index];

     }

}

具体迭代器类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

public class concreteiterator : iterator

{

     // 迭代器要集合对象进行遍历操作,自然就需要引用集合对象

     private concretelist _list;

     private int _index;

     public concreteiterator(concretelist list)

     {

         _list = list;

         _index = 0 ;

     }

     public bool movenext()

     {

         if (_index < _list.length)

         {

             return true ;

         }

         return false ;

     }

     public object getcurrent()

     {

         return _list.getelement(_index);

     }

     public void reset()

     {

         _index = 0 ;

     }

     public void next()

     {

         if (_index < _list.length)

         {

         _    index++;

         }

     }

}

客户端调用

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

class program

{

     static void main(string[] args)

     {

         iterator iterator;

         ilistcollection list = new concretelist();

         iterator = list.getiterator();

         while (iterator.movenext())

         {

             int i = ( int )iterator.getcurrent();

             console.writeline(i.tostring());

             iterator.next();

         }

         console.read();

     }

}

结果

六、net中迭代器模式的应用

在mscorlib程序集里有这样一个命名空间,该命名空间就是: system.collections ,在该命名空间里面早已有了迭代器模式的实现。对于聚集接口和迭代器接口已经存在了,其中ienumerator扮演的就是迭代器的角色,它的实现如下:

?

1

2

3

4

5

6

7

8

9

public interface ienumerator

  {

       object current

       {

            get;

       }

      bool movenext();

      void reset();

  }

属性current返回当前集合中的元素,reset()方法恢复初始化指向的位置,movenext()方法返回值true表示迭代器成功前进到集合中的下一个元素,返回值false表示已经位于集合的末尾。能够提供元素遍历的集合对象,在.net中都实现了ienumerator接口。

ienumerable则扮演的就是抽象聚集的角色,只有一个getenumerator()方法,如果集合对象需要具备跌代遍历的功能,就必须实现该接口。

?

1

2

3

4

public interface ienumerable

{

     ieumerator getenumerator();

}

抽象聚合角色(aggregate)和抽象迭代器角色(iterator)分别是ienumerable接口和ienumerator接口,具体聚合角色(concreteaggregate)有queue类型, bitarray等类型

七、总结

迭代器模式就是抽象一个迭代器类来分离了集合对象的遍历行为,这样既可以做到不暴露集合的内部结构,又可让外部代码透明地访问集合内部的数据

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!

原文链接:https://HdhCmsTestcnblogs测试数据/xuwendong/p/9898030.html

查看更多关于深入理解Java设计模式之迭代器模式的详细内容...

  阅读:14次