`
289196801
  • 浏览: 31084 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

JAVA迭代器与迭代模式

    博客分类:
  • J2EE
 
阅读更多
在jdk中,与迭代器相关的接口有两个:Iterator 与 Iterable

Iterator:迭代器,Iterator及其子类通常是迭代器本身的结构与方法;
Iterable:可迭代的,那些想用到迭代器功能的其它类,如AbstractList HashMap等,需要实现该接口。

以下为两个接口源码:
Iterator 如下:
 
1.public interface Iterator<E> {  
2.    boolean hasNext();  
3.    E next();  
4.    void remove();  
5.}  

Iterable 如下:
1.public interface Iterable<T> {  
2.    Iterator<T> iterator();  
3.}  


看完源码,我们来看看迭代器是如何使用的:
1. 若类A想要使用迭代器,则它的类声明部分为 class A implement Iterable
2. 在类A实现中,要实现Iterable接口中的唯一方法:Iterator<T> iterator();
   这个方法用于返回一个迭代器,即Iterator接口及其子类;
3. 在类A中,定义一个内部类S,专门用于实现Iterator接口,定制类A自已的迭代器实现。
如下:
1.class A implement Iterable  
2.{  
3.    Iterator<T> iterator() {...}  
4.    class S implement Iterator<E>   
5.    {  
6.        boolean hasNext() {....}  
7.        E next() {....}  
8.        void remove() {....}  
9.    }  
10.}  


我们再来看看jdk中是如何来使用的,如抽象类AbstractList:
1.public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> { // List接口实现了Collection<E>, Iterable<E> 
2.  
3.    protected AbstractList() {  
4.    }  
5.    
6.    ...  
7.  
8.    public Iterator<E> iterator() {  
9.    return new Itr();  // 这里返回一个迭代器
10.    }  
11.  
12.    private class Itr implements Iterator<E> {  // 内部类Itr实现迭代器
13.       
14.    int cursor = 0;  
15.    int lastRet = -1;  
16.    int expectedModCount = modCount;  
17.  
18.    public boolean hasNext() {  // 实现hasNext方法
19.            return cursor != size();  
20.    }  
21.  
22.    public E next() {  // 实现next方法
23.            checkForComodification();  
24.        try {  
25.        E next = get(cursor);  
26.        lastRet = cursor++;  
27.        return next;  
28.        } catch (IndexOutOfBoundsException e) {  
29.        checkForComodification();  
30.        throw new NoSuchElementException();  
31.        }  
32.    }  
33.  
34.    public void remove() {  // 实现remove方法
35.        if (lastRet == -1)  
36.        throw new IllegalStateException();  
37.            checkForComodification();  
38.  
39.        try {  
40.        AbstractList.this.remove(lastRet);  
41.        if (lastRet < cursor)  
42.            cursor--;  
43.        lastRet = -1;  
44.        expectedModCount = modCount;  
45.        } catch (IndexOutOfBoundsException e) {  
46.        throw new ConcurrentModificationException();  
47.        }  
48.    }  
49.  
50.    final void checkForComodification() {  
51.        if (modCount != expectedModCount)  
52.        throw new ConcurrentModificationException();  
53.    }  
54.    }  
55.}  




看完了迭代器,我们再看看迭代器模式:
1. 提出问题:什么是迭代器模式? 为什么会有迭代器模式?
   GOF给出的定义为:提供一种方法访问一个容器(container)对象中各个元素,而又不
                     需暴露该对象的内部细节。
    我的理解: 就是把遍历算法从容器对象中独立出来,为什么要把遍历算从容器对象中独立
              出来呢? 因为在面向对象设计中,一个难点就是辨认对象的职责。理想的状
              态下,一个类应该只有一个单一的职责。职责分离可以最大限度的去耦合,但
              是职责单一说起来容易,做起来难。具体到本模式,我们明显可以看到,一个
              容器对象它提供了两个职责:一是组织管理数据对象,二是提供遍历算法。
    所以:Iterator模式就是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样
         既可以做到不暴露集合的内部结构,又可让外部代码透明的访问集合内部的数据。

2. 下面开始讲迭代器模式:
   迭代器模式由以下角色组成:
  1) 迭代器角色(Iterator):迭代器角色负责定义访问和遍历元素的接口。
  2) 具体迭代器角色(Concrete Iterator):具体迭代器角色要实现迭代器接口,并
        要记录遍历中的当前位置。
    3) 容器角色(Container):容器角色负责提供创建具体迭代器角色的接口。
  4) 具体容器角色(Concrete Container):具体容器角色实现创建具体迭代器角色
        的接口——这个具体迭代器角色于该容器的结构相关。

    迭代器模式的类图如下:
   
  


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics