迭代器模式

1.迭代器模式是什么

1.百度百科

迭代器模式(Iterator Pattern)是 Java 和 .Net 编程环境中非常常用的设计模式。这种模式用于顺序访问集合对象的元素,不需要知道集合对象的底层表示。

2.维基百科

In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.

For example, the hypothetical algorithm SearchForElement can be implemented generally using a specified type of iterator rather than implementing it as a container-specific algorithm. This allows SearchForElement to be used on any container that supports the required type of iterator.

3.lz理解

4.核心角色

迭代器角色(Iterator):定义遍历元素所需要的方法,一般来说会有这么三个方法:取得下一个元素的方法next(),判断是否遍历结束的方法hasNext()),移出当前对象的方法remove(),

具体迭代器角色(Concrete Iterator):实现迭代器接口中定义的方法,完成集合的迭代。

容器角色(Aggregate): 一般是一个接口,提供一个iterator()方法,例如java中的Collection接口,List接口,Set接口等

具体容器角色(ConcreteAggregate):就是抽象容器的具体实现类,比如List接口的有序列表实现ArrayList,List接口的链表实现LinkList,Set接口的哈希列表的实现HashSet等。

2.迭代器模式解决了什么问题

简化遍历 对于集合对象的遍历,还是比较麻烦的,对于数组和类似数组的数据结构我们可以通过下标来遍历该元素。但是如果是Map、Set类型的元素呢。自己遍历非常麻烦,而引入迭代器方法后遍历就被简化了。

封装性良好 客户端只需获取该容器的迭代器就可以遍历,而不用关心如何遍历容器。

3.迭代器模式用法

数组的结构简单比较合适实现迭代器模式。我们就从数组来实现迭代器模式
抽象容器 一个list

public interface List {
	public void add(Object obj);

	public Object get(int index);

	public Iterator iterator();

	public int getSize();
}

具体容器,


public class ConcreteList implements List {

	private Object[] list;
	private int size = 0;
	private int index = 0;

	public ConcreteList() {
		index = 0;
		size = 0;
		list = new Object[100];
	}

	@Override
	public void add(Object obj) {
		list[this.index++] = obj;
		size++;
	}

	@Override
	public Object get(int index) {
		return list[index];
	}

	@Override
	public Iterator iterator() {
		return new ConcreteIterator(this);
	}

	@Override
	public int getSize() {
		return size;
	}

}

抽象迭代器

public interface Iterator {

	Boolean hasNext();

	Object next();

}

具体迭代器

public class ConcreteIterator implements Iterator {

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

	private List list = null;
	private int index;

	@Override
	public Boolean hasNext() {
		if (index >= list.getSize()) {
			return false;
		}
		return true;
	}

	@Override
	public Object next() {
		return list.get(index++);
	}

}

客户端调用

public class Client {

	public static void main(String[] args) {
		List list = new ConcreteList();
		list.add("fly");
		list.add("in");
		list.add("sky");
		list.add("!");
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
	}

}

结果

fly

in

sky

!

4.迭代器模式的问题

迭代器遍历复杂 对于比较简单的遍历(像数组或者有序列表),使用迭代器方式遍历较为繁琐,大家可能都有感觉,像ArrayList,我们宁可愿意使用for循环和get方法来遍历集合。

无法通用 由于迭代器模式将存储数据和遍历数据的职责分离,增加新的聚合类需要对应增加新的迭代器类,类的个数成对增加,这在一定程度上增加了系统的复杂性。

5.迭代器模式总结

使用场景:
1.访问一个容器的内容而无需暴露它的内部表示
2.支持对容器的多种类型的遍历
3.为遍历不同的聚合结构提供一个统一的接口

碎碎念:
迭代器模式这个模式我们用的很少,一般迭代器都会伴随着容器的创建而一起创建出来。而常用的容器基本都已经实现了迭代器。所以迭代器模式是我们理解JDK集合类的一把钥匙。然而也仅仅只是钥匙而已,从实现的角度上来讲极少的机会使用到这种设计方法。

引用

https://www.cnblogs.com/ysw-go/p/5384516.html
https://www.cnblogs.com/chenssy/p/3250409.html

原文地址:https://www.cnblogs.com/yanlong300/p/8421274.html