设计模式

迭代器模式:提供一种方法访问一个容器对象中各个元素,而又不需暴露该对象的内部细节。

迭代器模式中的角色

抽象迭代器:抽象迭代器负责定义访问和遍历元素的接口。迭代器的抽象是为了使迭代器不依赖于容器的内部结构。

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

抽象容器:抽象容器负责提供创建迭代器的接口。

具体容器:具体容器要实现创建具体迭代器的接口,这个具体迭代器与该具体容器的内部结构相关。

类图

代码实现

抽象迭代器:

package com.huey.pattern.iterator;

/**
 * the iterator interface
 * @author  huey
 * @version 1.0 
 * @created 2015-11-26
 * @param <E>
 */
public interface Iterator<E> {

    public boolean hasNext();
    
    public E next();
    
}

具体迭代器:

package com.huey.pattern.iterator;

import java.util.Arrays;

/**
 * the concrete iterator
 * @author  huey
 * @version 1.0 
 * @created 2015-11-26
 * @param <E>
 */
public class SortedIterator<E> implements Iterator<E> {
    
    private E[] items;
    int index = 0;
    
    public SortedIterator(E[] items) {
        this.items = items;
        if (items != null && items.length > 0 && items[0] != null 
                && items[0] instanceof Comparable<?>) {
            Arrays.sort(this.items);
        }
    }
    
    @Override
    public boolean hasNext() {
        if (items == null || index >= items.length || items[index] == null) {
            return false;
        }
        return true;
    }
    
    @Override
    public E next() {
        E item = items[index++];
        return (E) item;
    }
    
}

抽象容器:

package com.huey.pattern.iterator;

/**
 * the container interface
 * @author  huey
 * @version 1.0 
 * @created 2015-11-26
 * @param <E>
 */
public interface Collection<E> {
    
    public boolean add(E e);
    
    /**
     * get the iterator
     * @return
     */
    public Iterator<E> iterator();
    
}

具体容器:

package com.huey.pattern.iterator;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * the concrete container
 * @author  huey
 * @version 1.0 
 * @created 2015-11-26
 * @param <E>
 */
public class SortedList<E> implements Collection<E> {
    
    private E[] items;
    private int size;
    
    @SuppressWarnings("unchecked")
    public SortedList(Class<E> type) {
        items = (E[]) Array.newInstance(type, 10);
        size = 0;
    }
    
    public boolean add(E e) {
        ensureCapacity(size + 1);
        items[size++] = e;
        return true;
    }
    
    @Override
    public Iterator<E> iterator() {
        return new SortedIterator<E>(items);
    }
    
    public void ensureCapacity(int minCapacity) {
        int oldCapacity = items.length;
        if (minCapacity > oldCapacity) {
            int newCapacity = (oldCapacity * 3) / 2 + 1;
            if (newCapacity < minCapacity)
                newCapacity = minCapacity;
            // minCapacity is usually close to size, so this is a win:
            items = Arrays.copyOf(items, newCapacity);
        }
    }
}

单元测试:

package com.huey.pattern.iterator;

public class IteratorPatternTest {

    public static void main(String[] args) {        
        Collection<Long> container = new SortedList<Long>(Long.class);
        container.add(2L);
        container.add(0L);
        container.add(1L);
        container.add(5L);
        
        Iterator<Long> iterator = container.iterator();
        while (iterator.hasNext()) {
            Long item = iterator.next();
            System.out.println(item);
        }
    }
}

结果输出:

0
1
2
5

迭代器模式的适用场合

1) 访问一个集合对象的内容,而无须暴露它的内部表示。

2) 支持对集合对象的多种遍历方式。

3) 为遍历不同的集合对象结构提供一个统一的接口。

Java SDK 中的迭代器模式

迭代器对于集合容器类的遍历非常有效和实用,在 JDK 中,已经定义了 java.util.Iterator、java.lang.Iterable、java.util.Collection 等接口。

原文地址:https://www.cnblogs.com/huey/p/4997338.html