长葛网站建设,网页平台,网站建设公司中企动力强,天津网络广告公司目录 1.概述2.结构3.案例实现3.1.抽象迭代器3.2.具体迭代器3.3.抽象聚合3.4.具体聚合3.5.测试 4.优缺点5.使用场景6.JDK 源码解析——Iterator 1.概述
迭代器模式 (Iterator Pattern) 是一种行为型设计模式#xff0c;它提供一种顺序访问聚合对象#xff08;如列表、集合等它提供一种顺序访问聚合对象如列表、集合等中的元素而无需暴露聚合对象的内部表示。迭代器模式将遍历逻辑封装在一个迭代器对象中使得我们可以使用统一的方式遍历不同类型的聚合对象同时也可以简化客户端代码。
2.结构
迭代器模式主要包含以下角色
抽象聚合 (Aggregate) 角色定义存储、添加、删除聚合元素以及创建迭代器对象的接口。具体聚合 (ConcreteAggregate) 角色实现抽象聚合类返回一个具体迭代器的实例。抽象迭代器 (Iterator) 角色定义访问和遍历聚合元素的接口通常包含 hasNext()、next() 等方法。具体迭代器 (Concretelterator) 角色实现抽象迭代器接口中所定义的方法完成对聚合对象的遍历记录遍历的当前位置。
3.案例实现
【例】定义一个可以存储学生对象的容器对象将遍历该容器的功能交由迭代器实现其类图如下
具体实现代码如下 Student.java
public class Student {private String name;private String number;public Student() {}public Student(String name, String number) {this.name name;this.number number;}Overridepublic String toString() {return Student{ name name \ , number number \ };}public String getName() {return name;}public void setName(String name) {this.name name;}public String getNumber() {return number;}public void setNumber(String number) {this.number number;}
}3.1.抽象迭代器
StudentIterator.java
//抽象迭代器角色接口
public interface StudentIterator {//判断是否还有元素boolean hasNext();//获取下一个元素Student next();
}3.2.具体迭代器
StudentIteratorImpl.java
//具体迭代器角色类
public class StudentIteratorImpl implements StudentIterator{private ListStudent list;//用来记录遍历时的位置private int position 0;public StudentIteratorImpl(ListStudent list) {this.list list;}Overridepublic boolean hasNext() {return position list.size();}Overridepublic Student next() {//从集合中或者去指定位置的元素Student currentStudent list.get(position);position;return currentStudent;}
}3.3.抽象聚合
StudentAggregate.java
//抽象聚合(容器)角色接口
public interface StudentAggregate {//添加学生void addStudent(Student stu);//删除学生void removeStudent(Student stu);//获取迭代器对象StudentIterator getStudentIterator();
}3.4.具体聚合
StudentAggregateImpl.java
public class StudentAggregateImpl implements StudentAggregate{private ListStudent list new ArrayList();Overridepublic void addStudent(Student stu) {list.add(stu);}Overridepublic void removeStudent(Student stu) {list.remove(stu);}//获取迭代器对象Overridepublic StudentIterator getStudentIterator() {return new StudentIteratorImpl(list);}
}3.5.测试
Client.java
public class Client {public static void main(String[] args) {//创建聚合(容器)对象StudentAggregateImpl aggregate new StudentAggregateImpl();Student student1 new Student(Tom, 1001);Student student2 new Student(Mike, 1002);Student student3 new Student(Jerry, 1003);Student student4 new Student(Mary, 1004);//添加元素aggregate.addStudent(student1);aggregate.addStudent(student2);aggregate.addStudent(student3);aggregate.addStudent(student4);//删除元素aggregate.removeStudent(student3);//遍历聚合对象// 1.获取迭代器对象StudentIterator iterator aggregate.getStudentIterator();// 2.遍历while (iterator.hasNext()){// 3.获取元素Student student iterator.next();System.out.println(student.toString());}}
}结果如下
Student{nameTom, number1001}
Student{nameMike, number1002}
Student{nameMary, number1004}4.优缺点
1迭代器模式的优缺点如下
优点 简化客户端代码迭代器模式将遍历逻辑封装在迭代器对象中客户端代码只需通过迭代器接口与迭代器对象交互无需关心具体的遍历算法和聚合对象的内部结构从而简化了客户端代码。统一遍历接口迭代器模式提供一种统一的遍历接口使得对不同类型的聚合对象可以使用相同的遍历方式提供了更加灵活和通用的遍历操作。支持多种遍历方式通过不同的迭代器实现迭代器模式支持多种遍历方式如顺序遍历、逆序遍历等使得遍历操作更加灵活可变。解耦聚合对象与遍历操作迭代器模式将遍历操作从聚合对象中分离出来使得聚合对象的内部结构不再暴露给客户端遵循了单一职责原则提高了代码的可维护性和可扩展性。 缺点 对于某些较为复杂的聚合对象实现迭代器可能会比较复杂需要实现多个方法并考虑并发访问的线程安全性增加了代码的复杂性。当遍历的聚合对象发生变化时需要相应地修改迭代器的实现可能会影响到迭代器的逻辑。
2总体来说迭代器模式适用于需要统一遍历接口、封装遍历逻辑、解耦聚合对象与遍历操作的场景。它可以提供更加灵活、通用和可维护的遍历操作但在某些情况下可能会增加代码的复杂性。因此在使用迭代器模式时需要综合考虑具体的设计需求和项目情况。
5.使用场景
1迭代器模式通常适用于以下场景
需要遍历聚合对象的元素当需要遍历并访问聚合对象中的元素并且不希望暴露聚合对象的内部结构给客户端时可以使用迭代器模式。它可以将遍历操作封装在迭代器中提供一个统一的遍历接口。需要对不同类型的聚合对象使用统一的遍历方式迭代器模式提供了一种统一的遍历接口使得对不同类型的聚合对象可以使用相同的遍历方式。这样可以增加代码的灵活性和可维护性使得客户端代码更加通用。需要支持多种遍历方式如果需要支持多种遍历方式如顺序遍历、逆序遍历等可以通过不同的迭代器实现来实现不同的遍历方式。这样可以使遍历操作更加灵活可变。需要屏蔽聚合对象的内部实现细节当聚合对象的内部结构较为复杂或者不希望暴露内部结构给客户端时可以使用迭代器模式进行封装。迭代器模式将遍历逻辑封装在迭代器对象中客户端无需关心聚合对象的具体实现细节。需要遍历操作与聚合对象解耦迭代器模式将遍历操作从聚合对象中分离出来使得聚合对象的内部结构不再暴露给客户端。这样可以提高代码的可维护性和可扩展性使遍历操作与聚合对象解耦。
2迭代器模式在现实生活中的应用也非常广泛例如集合类、数据库查询结果等都可以使用迭代器模式来统一遍历操作接口。
6.JDK 源码解析——Iterator
1迭代器模式在 Java 的很多集合类中被广泛应用接下来看下面的例子
public class TestArratList {public static void main(String[] args) {ListString list new ArrayList();//添加元素list.add(1001);list.add(1002);list.add(1003);//删除元素list.remove(1003);IteratorString iterator list.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}
}结果如下
1001
10022看完这段代码是不是觉得很熟悉它与我们上面的代码基本类似。单列集合都使用到了迭代器所以现在以 ArrayList 举例来说明
List抽象聚合类ArrayList具体的聚合类Iterator抽象迭代器list.iterator()返回的是实现了 Iterator 接口的具体迭代器对象
3具体来看看 ArrayList 的部分关键代码实现
public class ArrayListE extends AbstractListEimplements ListE, RandomAccess, Cloneable, java.io.Serializable {public IteratorE iterator() {return new Itr();}private class Itr implements IteratorE {int cursor; // 下一个要返回元素的索引int lastRet -1; // 上一个返回元素的索引int expectedModCount modCount;Itr() {}//判断是否还有元素public boolean hasNext() {return cursor ! size;}//获取下一个元素public E next() {checkForComodification();int i cursor;if (i size)throw new NoSuchElementException();Object[] elementData ArrayList.this.elementData;if (i elementData.length)throw new ConcurrentModificationException();cursor i 1;return (E) elementData[lastRet i];}//...
}4这部分代码大致就是在 iterator 方法中返回了一个实例化的 Iterator 对象。Itr 是一个内部类它实现了 Iterator 接口并重写了其中的抽象方法。此外通过下图可知 ArrayList 与 Iterable 的关系 package java.lang;import java.util.Iterator;
import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;public interface IterableT {IteratorT iterator();default void forEach(Consumer? super T action) {Objects.requireNonNull(action);for (T t : this) {action.accept(t);}}default SpliteratorT spliterator() {return Spliterators.spliteratorUnknownSize(iterator(), 0);}
}5当我们在使用 Java 开发的时想使用迭代器模式的话只要让我们自己定义的容器类实现 java.util.Iterable 并实现其中的 iterator() 方法使其返回一个 java.util.Iterator 的实现类就可以了。