設計模式-迭代器模式

迭代器模式(Iterator Pattern),提供一種方法順序訪問一個聚合對象中各個元素,而又不暴露該對象的內部。迭代器模式屬於行爲型模式。java

模式定義

迭代器模式就是分離了集合對象的遍歷行爲,抽象出一個迭代器類來負責,這樣既能夠作到不暴露集合的內部結構,又可以讓外部代碼透明地訪問集合內部的數據。git

模式結構

首先看下結構圖:github

Iterator迭代器接口類數據結構

public interface Iterator {

    //獲得下一個對象
    Object next();

    //判斷是否有下一個對象
    boolean hasNext();

}

Aggregate彙集抽象類ide

public interface Aggregate {
    
    //添加
    void add(Object object);

    //建立迭代器
    Iterator createIterator();

}

ConcreteIterator具體迭代器類,實現Iteratorthis

public class ConcreteIterator implements Iterator {

    private List list;
    private int currentIndex = 0;

    public ConcreteIterator(List list) {
        this.list = list;
    }

    @Override
    public Object next() {
        if (hasNext()) {
            return list.get(currentIndex++);
        }
        return null;
    }

    @Override
    public boolean hasNext() {
        if (currentIndex == list.size()) {
            return false;
        }
        return true;
    }

}

ConcreteAggregate集體彙集類,繼承Aggregate3d

public class ConcreteAggregate implements Aggregate {

    private List<Object> items = new ArrayList<>();

    @Override
    public Iterator createIterator() {
        return new ConcreteIterator(items);
    }

    public void add(Object object) {
        items.add(object);
    }

}

客戶端代碼code

public class Client {

    public static void main(String[] args) {
        Aggregate aggregate = new ConcreteAggregate();
        aggregate.add(1);
        aggregate.add(2);
        aggregate.add(3);
        aggregate.add(4);

        Iterator iterator = aggregate.createIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

    }

}

運行結果對象

1
2
3
4

模式實現

在java中有一個Iterable接口,實現該接口的類就是可迭代的,而且支持加強for循環。該接口只有一個方法即獲取迭代器的方法iterator(),能夠獲取每一個容易自身的迭代器Iterator。blog

public interface Iterable<T> {
    //返回迭代器對象
    Iterator<T> iterator();
}

Iterator接口中的方法。

public interface Iterator<E> {
    //是否有下一個元素
    boolean hasNext();
    //下一個元素
    E next();
    //將刪除上次調用next方法時返回的元素,若是想要刪除指定位置上的元素,須要越過這個元素
    //next方法和remove方法是相互依賴的,若是調用remove方法以前沒有調用next將是不合法的,
    //將會拋出IllegalStateException異常
    void remove();
}

Collection繼承了Iterable接口,因此Collection體系都具有獲取自身迭代器的方法,只不過每一個子類集合都進行了重寫(由於數據結構不一樣)。

接下來咱們主要看下ArrayList中是怎麼實現的。

//調用ArrayList的該方法返回內部實現Iterator接口的Itr對象
    public Iterator<E> iterator() {
        return new Itr();
    }

   private class Itr implements Iterator<E> {
        //下一個元素的下標
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        Itr() {}
        
        public boolean hasNext() {
            //若是下一個元素的下表不等於容器中元素的實際大小,返回true表明有下一個元素
            return cursor != size;
        }

        @SuppressWarnings("unchecked")
        public E next() {
            //檢查被迭代的對象是否被修改過
            checkForComodification();
            int i = cursor;
            //若是下標大於等於容器中元素的實際大小拋出異常
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            //若是下標大於等於列表長度,拋出異常
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            //更新下個元素的下標
            cursor = i + 1;
            //返回列表中當前下標的元素,並對lastRet賦值
            return (E) elementData[lastRet = i];
        }

        public void remove() {
            //若是lastRet小於0表明沒調用過next()方法,拋出異常
            if (lastRet < 0)
                throw new IllegalStateException();
            //檢查被迭代的對象是否被修改過
            checkForComodification();

            try {
                //根據調用next()給lastRet賦值的下標去刪除元素
                ArrayList.this.remove(lastRet);
                //由於刪除元素後容器內元素實際大小減1,給cursor賦值未加1前的下標
                cursor = lastRet;
                //lastRet重置
                lastRet = -1;
                //更新expectedModCount的值
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
        
        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }

checkForComodification()方法檢查所迭代的列表對象是否被修改過,modCount是外部類的一個字段,當調用外部類的add,remove,ensureCapacity等方法,都會改變該字段的值,而expectedModCount是內部類Itr初始化時,使用modCount賦值的字段。這樣,在使用迭代器過程當中,若是正在對正在迭代的對象調用了add,remvoe,ensureCapacity等方法,再去調用迭代器的next方法,就會引起ConcurrentModificationException異常了。

代碼示例

@Test
    public void test2() {
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        
        Iterator iterator = list.iterator();
        //while遍歷
        while (iterator.hasNext()) {
            System.out.println(iterator.next());

        }
    }

運行結果

1
2
3

若是如上面所說在循環過程當中修改list的列表對象,看是否會引起異常。咱們隊上面代碼簡單修改一下。

@Test
    public void test2() {
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);

        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
            list.add(6);
        }
    }
1

java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
    at java.util.ArrayList$Itr.next(ArrayList.java:859)
    at com.lwx.arraylist.ArrayListTest.test2(ArrayListTest.java:40)

能夠看出在遍歷第二個元素調用next()方法時,就會引起異常。

模式的優缺點

優勢

  • 在迭代器模式中,增長新的聚合類和迭代器類都很方便,無須修改原有代碼。
  • 它支持以不一樣的方式遍歷一個聚合對象。

缺點

  • 因爲迭代器模式將存儲數據和遍歷數據的職責分離,增長新的聚合類須要對應增長新的迭代器,類的個數成對增長,這在必定程度上增長了系統的複雜性。

模式使用場景

當你須要對彙集有多種方式遍歷時,能夠考慮用迭代器模式。不過大多數高級語言都對它進行了封裝,因此給人感受這種模式自己不太經常使用了。

文章做者:leisurexi

新博客地址:https://leisurexi.github.io

許可協議: 署名-非商業性使用-禁止演繹 4.0 國際 轉載請保留原文連接及做者。

相關文章
相關標籤/搜索