描述
- 提供一个对象来顺序访问聚合列表中的元素,不暴露聚合列表中对象的内部表示。
- 遍历单例集合时,经常使用到(Collection.iterator)。
角色
- 抽象聚合角色:存储,添加,删除聚合原始的抽象方法。
- 具体聚合角色:集合,容器。实现抽象聚合角色,并返回具体迭代器实例。
- 抽象迭代器角色:定义遍历聚合元素的接口,通常包括hasNest(),next()。
- 具体迭代器角色:实现抽象迭代器角色,完成聚合对象的遍历。
实现
public class Test {
public static void main(String[] args) {
Peoples peoples = new Peoples();
peoples.add(new People("阿椰", 25));
peoples.add(new People("阿明", 22));
peoples.add(new People("阿发", 23));
MyIterator iterator = peoples.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
class People {
private String name;
private Integer age;
People(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
interface AbstractPeoples {
void add(People people);
void remove(People people);
MyIterator iterator();
}
class Peoples implements AbstractPeoples {
private final List<People> peopleList = new ArrayList<>();
@Override
public void add(People people) {
peopleList.add(people);
}
@Override
public void remove(People people) {
peopleList.remove(people);
}
@Override
public MyIterator iterator() {
return new PeopleIterator(peopleList);
}
}
interface MyIterator {
boolean hasNext();
People next();
}
class PeopleIterator implements MyIterator {
private int point = 0;
private List<People> peopleList;
PeopleIterator(List<People> peopleList) {
this.peopleList = peopleList;
}
@Override
public boolean hasNext() {
return peopleList.size() > point;
}
@Override
public People next() {
return peopleList.get(point++);
}
}
优点
- 支持不同的方式(定义不同的具体迭代角色)遍历聚合对象。
- 遍历方式和聚合角色松耦合,满足开闭原则(新增迭代器,不改原有代码)。
缺点
使用场景
- 当聚合对象需要切换或拥有多种遍历方式时,可以使用迭代器模式。
- 当要为不同的聚合对象,提供一个统一的遍历方式时,可以使用迭代器模式。
- 当遍历聚合对象,且要隐藏聚合对象中对象的细节时,可以使用迭代器模式。
JDK中的应用
- 单列集合,如 List
- 抽象聚合角色:List
- 具体聚合角色:ArrayList,LinkedList等
- 抽象迭代器角色:Iterator
- 具体迭代器角色:list.iterator() =》 Ite类
![具体迭代器角色 Itr](https://i-blog.csdnimg.cn/blog_migrate/6946a45101f947e458c6369f6ec3acdd.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4fcedabf49cb455df346398091bd9b99.png)
JAVA中迭代器模式的使用
- 我们要使用迭代器模式的话,可以让我们定义的容器去实现java.util.Iterable接口,并重写iterator()方法,返回Iterable实现类即可。
- 如下:
public class Test {
public static void main(String[] args) {
PeopleList peopleList = new PeopleList();
peopleList.add(new People("阿椰", 25));
peopleList.add(new People("阿明", 22));
peopleList.add(new People("阿发", 23));
Iterator iterator = peopleList.jdkIterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
class PeopleList implements AbstractPeoples {
private final List<People> peopleList = new ArrayList<>();
@Override
public void add(People people) {
peopleList.add(people);
}
@Override
public void remove(People people) {
peopleList.remove(people);
}
@Override
public MyIterator iterator() {
return new PeopleIterator(peopleList);
}
public Iterator jdkIterator() {
return new PeopleItr(peopleList);
}
private class PeopleItr implements Iterator<People> {
private int point = 0;
private List<People> peopleList;
PeopleItr(List<People> peopleList) {
this.peopleList = peopleList;
}
@Override
public boolean hasNext() {
return peopleList.size() > point;
}
@Override
public People next() {
return peopleList.get(point++);
}
}
}