什么是迭代器模式?
迭代器模式是一种行为模式,它提供了一种顺序访问聚合(集合)对象的元素而不暴露其底层表示的方法。
什么时候使用它?
- 当您不想公开集合的数据结构时,请使用迭代器模式。
- 用户迭代器模式,当您希望一个通用接口来遍历不同的数据结构,而不是为应用程序中的每个数据结构使用许多类似的遍历代码时。
问题
公司“alpha inc.”将与“beta inc.”合并。我们在人力资源部门,必须将他们的员工数据合并在一起。
employeelista 保存 alpha 员工的数据,employeelistb 保存 beta 员工的数据。因为employeelista使用array数据结构,employeelistb使用arraylist,所以我们写了两段遍历代码

public class hrdepartment {
public void printemployee(){
employeelista employeelista = new employeelista("alpha inc");
string[] alphaemployee = employeelista.getemployees();
employeelistb employeelistb = new employeelistb("beta inc");
list betaemployee = employeelistb.getemployees();
// traversal code for array
for (int i = 0; i < alphaemployee.length; i++) {
system.out.println(alphaemployee[i] + " from alpha inc.");
}
// traversal code for arraylist
for (int i = 0; i < betaemployee.size(); i++) {
system.out.println(betaemployee.get(i) + " from beta inc.");
}
}
}
你能发现问题吗?这是我们的代码当前存在的问题:
- 我们正在编码具体实现(employeelista 和 employeelistb)而不是接口。
- 如果我们决定将 employeelista 数据结构从 array 切换到另一种数据结构(例如哈希映射),我们需要大量修改 hrdepartment 类。
- hrdepartment 需要知道聚合的内部结构,换句话说,我们正在公开集合的数据结构。
- 对于不同的数据结构,array 和 arraylist,我们有重复的遍历代码。
解决方案
封装迭代逻辑
那么从哪里开始呢?让我们删除重复的迭代代码。
如果我们能封装迭代逻辑,引入一个通用的接口,让hr部门只用一个接口就可以迭代任何数据结构,那就太好了。

我们需要 iterator 接口来将迭代逻辑与 hrdepartment 解耦。
public interface iterator {
boolean hasnext();
string next();
}
hasnext() 返回布尔值,指示集合是否有下一个元素。 next() 返回集合中的下一个元素。 (稍后你会看到实际的实现)
在 employeelista/employeelista 中,我们删除了 getemployees() 方法,因为它公开了聚合的数据结构。重要的是,我们编写了新方法 createiterator(),它创建了相应的具体 iterator,例如 employeelista.createiterator() 实例化 employeelistaiterator。
引入聚合的通用接口
好的,我们现在封装了迭代逻辑,我们的聚合不暴露其底层表示,删除了重复的遍历代码。但仍然存在一个问题,人力资源部门依赖于具体的骨料。让我们介绍一个混凝土骨料的通用接口。

结构

java实现
我将省略 employeelistbiterator 和 employeelistb 的实现,因为它们与 employeelistaiterator 和 employeelista 的实现类似。如果您不确定如何实现它们,可以查看我的 github 存储库(我的存储库的链接位于本博客末尾)。
public interface iterator {
boolean hasnext();
string next();
}
public class employeelistaiterator implements iterator {
private string[] employees;
private int index = 0;
public employeelistaiterator(string[] employees) {
this.employees = employees;
}
@override
public boolean hasnext() {
if (index >= employees.length || employees[index] == null) {
return false;
}
return true;
}
@override
public string next() {
string employee = employees[index];
index++;
return employee;
}
}
public interface employeelist {
iterator createiterator();
string getcompanyname();
}
public class employeelista implements employeelist{
private string companyname;
private string[] employees;
private int size = 5;
private int index = 0;
public employeelista(string companyname) {
this.companyname = companyname;
employees = new string[size];
addemployee("alice");
addemployee("alisha");
addemployee("alex");
}
public void addemployee(string name) {
employees[index] = name;
index++;
}
public iterator createiterator() {
return new employeelistaiterator(employees);
}
public string getcompanyname() {
return companyname;
}
}
public class hrdepartment {
employeelist lista;
employeelist listb;
public hrdepartment(employeelist lista, employeelist listb) {
this.lista = lista;
this.listb = listb;
}
public void printemployee() {
iterator listaiterator = lista.createiterator();
iterator listbiterator = listb.createiterator();
system.out.println("--- employees from " + lista.getcompanyname() + " ---");
printemployee(listaiterator);
system.out.println("--- employees from " + listb.getcompanyname() + " ---");
printemployee(listbiterator);
}
private void printemployee(iterator iterator) {
while (iterator.hasnext()) {
system.out.println(iterator.next());
}
}
}
输出:
--- Employees from Alpha Inc --- Alice Alisha Alex --- Employees from Beta Inc --- Bob Bella Benjamin
陷阱
- 迭代器本身并不意味着迭代期间访问元素的顺序。迭代的顺序取决于正在遍历的底层数据结构,而不是迭代器。
- 迭代器不支持通过索引访问元素。
您可以在这里查看所有设计模式的实现。
github 存储库










