迭代器模式是一种行为模式,它提供了一种顺序访问聚合(集合)对象的元素而不暴露其底层表示的方法。
公司“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<string> 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.");
}
}
}
你能发现问题吗?这是我们的代码当前存在的问题:
那么从哪里开始呢?让我们删除重复的迭代代码。
如果我们能封装迭代逻辑,引入一个通用的接口,让hr部门只用一个接口就可以迭代任何数据结构,那就太好了。

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


我将省略 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 存储库
以上就是迭代器模式的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号