设计模式之迭代器模式
设计模式 Java About 4,109 words作用
提供一种遍历集合元素的统一接口,用一致的方法遍历集合元素,不需要知道集合对象的底层表示,不暴露其内部的结构。
原理
Iterator:迭代器接口,hasNext
、next
、remove
。
ConcreteIterator:具体迭代器类。
Aggregate:统一聚合接口,讲客户端与具体聚合解耦。
ConcreteAggregate:具体的聚合持有对象集合,并提供一个方法,返回一个迭代器,该迭代器可以正确遍历集合。
Client:客户端,通过Iterator和Aggregate依赖子类。
案例
Iterator迭代器接口
java.util.Iterator
ConcreteIterator具体迭代器类
public class ComputerCollegeIterator implements Iterator<Department> {
private Department[] departments;
private int position = 0;
public ComputerCollegeIterator(Department[] departments) {
this.departments = departments;
}
@Override
public boolean hasNext() {
if (position >= departments.length || departments[position] == null) {
return false;
}
return true;
}
@Override
public Department next() {
Department department = departments[position];
position += 1;
return department;
}
}
public class InfoCollegeIterator implements Iterator<Department> {
private List<Department> departments;
private int index = -1;
public InfoCollegeIterator(List<Department> departments) {
this.departments = departments;
}
@Override
public boolean hasNext() {
if (index >= departments.size() - 1) {
return false;
} else {
index += 1;
return true;
}
}
@Override
public Department next() {
return departments.get(index);
}
}
Aggregate统一聚合接口
public interface College {
String getName();
void addDepartment(String name, String desc);
Iterator createIterator();
}
ConcreteAggregate具体聚合持有对象集合
public class ComputerCollege implements College {
private Department[] departments;
private int numOfDepartment = 0;
public ComputerCollege() {
departments = new Department[5];
addDepartment("Java专业", "Java专业");
addDepartment("PHP专业", "PHP专业");
addDepartment("大数据专业", "大数据专业");
}
@Override
public String getName() {
return "计算机学院";
}
@Override
public void addDepartment(String name, String desc) {
Department department = new Department();
department.setName(name);
department.setDesc(desc);
departments[numOfDepartment] = department;
numOfDepartment += 1;
}
@Override
public Iterator createIterator() {
return new ComputerCollegeIterator(departments);
}
}
public class InfoCollege implements College {
private List<Department> departments;
public InfoCollege() {
departments = new ArrayList<>();
addDepartment("信息安全专业", "信息安全专业");
addDepartment("网络安全专业", "网络安全专业");
addDepartment("服务器安全专业", "服务器安全专业");
}
@Override
public String getName() {
return "信息学院";
}
@Override
public void addDepartment(String name, String desc) {
Department department = new Department();
department.setName(name);
department.setDesc(desc);
departments.add(department);
}
@Override
public Iterator createIterator() {
return new InfoCollegeIterator(departments);
}
}
Client通过Iterator和Aggregate依赖子类
public class Client {
public static void main(String[] args) {
List<College> colleges = new ArrayList<>();
ComputerCollege computerCollege = new ComputerCollege();
InfoCollege infoCollege = new InfoCollege();
colleges.add(computerCollege);
colleges.add(infoCollege);
Iterator<College> iterator = colleges.iterator();
while (iterator.hasNext()) {
College college = iterator.next();
System.out.println("=== " + college.getName() + "=====");
Iterator collegeIterator = college.createIterator();
while (collegeIterator.hasNext()) {
Department d = (Department) collegeIterator.next();
System.out.println(d.getName());
}
}
}
}
源码
java.util.ArrayList
具体迭代器:java.util.ArrayList.Itr
聚合接口,返回迭代器:java.util.List
Views: 3,029 · Posted: 2019-12-27
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...