一、简介
1、定义
迭代器模式(Iterator Pattern)是一种面向集合对象而生的行为设计模式。对于集合对象而言,会涉及对集合的添加和删除操作,也要支持遍历集合元素的操作。可以把遍历操作放在集合对象中,但这样做,集合对象就承担太多的责任了。面向对象设计原则中有一条就是单一职责原则,所以要尽可能地分离这些职责,用不同的类承担不同的责任,迭代器模式就是用迭代器类来承担遍历集合的职责。
迭代器模式提供了一种方法顺序访问一个集合对象中的各个元素,而又无需暴露该集合对象的内部实现,这样既可以做到不暴露集合对象的内部结构,又可让外部代码透明地访问集合内部的数据。
2、命令模式的结构
迭代模式涉及以下几个角色:
迭代器(Iterator)接口:定义访问和遍历集合元素的抽象方法,通常包含 hasNext()、next() 等方法。
具体迭代器(Concretelterator)类:实现迭代器接口中所定义的方法,完成对集合对象的遍历,记录遍历的当前位置。
聚合(Aggregate)类:构建集合数据,返回一个具体迭代器的实例。
客户端(Client)类:调用聚合类,维护集合对象,通过迭代器遍历集合对象。
二、Java实现案例
1、概念实现案例
/**
* @Description: 迭代器接口
* @Date: 2025-01-10 10:41
* @Author: gaoyufei
**/
public interface Iterator {
boolean hasNext();
Object next();
}
import java.util.List;
/**
* @Description: 具体迭代器类
* @Date: 2025-01-10 10:42
* @Author: gaoyufei
**/
public class ConcreteIterator implements Iterator{
private List<Object> items;
private int position;
public ConcreteIterator(List<Object> items){
this.items=items;
}
@Override
public boolean hasNext() {
return position<items.size();
}
@Override
public Object next() {
return items.get(position++);
}
}
import java.util.ArrayList;
import java.util.List;
/**
* @Description: 聚合类
* @Date: 2025-01-10 10:46
* @Author: gaoyufei
**/
public class Aggregate {
private List<Object> items=new ArrayList<>();
public void add(Object item){
items.add(item);
}
public Iterator iterator(){
return new ConcreteIterator(items);
}
}
/**
* @Description: 客户端
* @Date: 2025-01-10 10:48
* @Author: gaoyufei
**/
public class Client {
public static void main(String[] args) {
Aggregate aggregate=new Aggregate();
aggregate.add("item 1");
aggregate.add("item 2");
Iterator iterator = aggregate.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
2、书架遍历案例
/**
* @Description: 书
* @Date: 2025-01-10 13:07
* @Author: gaoyufei
**/
public class Book {
private String name;
public Book(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/**
* @Description: 迭代器接口
* @Date: 2025-01-10 13:08
* @Author: gaoyufei
**/
public interface Iterator<E> {
boolean hasNext();
E next();
}
/**
* @Description: 书架迭代器类
* @Date: 2025-01-10 13:10
* @Author: gaoyufei
**/
public class BookShelfIterator implements Iterator{
private BookShelf bookShelf;
private int index;
public BookShelfIterator(BookShelf bookShelf){
this.bookShelf=bookShelf;
this.index=0;
}
@Override
public boolean hasNext() {
return index<bookShelf.getBooks().size();
}
@Override
public Book next() {
return bookShelf.getBooks().get(index++);
}
}
import java.util.ArrayList;
import java.util.List;
/**
* @Description: 书架(聚合类)
* @Date: 2025-01-10 13:12
* @Author: gaoyufei
**/
public class BookShelf {
private List<Book> books=new ArrayList<>();
public void add(Book book){
books.add(book);
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public Iterator iterator(){
return new BookShelfIterator(this);
}
}
/**
* @Description: 客户端
* @Date: 2025-01-10 13:14
* @Author: gaoyufei
**/
public class Client {
public static void main(String[] args) {
BookShelf bookShelf=new BookShelf();
bookShelf.add(new Book("思考致富"));
bookShelf.add(new Book("思考,快与慢"));
Iterator iterator = bookShelf.iterator();
while (iterator.hasNext()){
Book book = (Book)iterator.next();
System.out.println(book.getName());
}
}
}
3、班级学生遍历案例
import java.util.Objects;
/**
* @Description: 学生类
* @Date: 2025-01-10 13:42
* @Author: gaoyufei
**/
public class Student {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}
/**
* @Description: 迭代器接口
* @Date: 2025-01-10 13:43
* @Author: gaoyufei
**/
public interface Iterator<E> {
boolean hasNext();
E next();
}
import java.util.List;
/**
* @Description: 学生迭代器实现
* @Date: 2025-01-10 13:44
* @Author: gaoyufei
**/
public class StudentIterator implements Iterator{
private List<Student> students;
private int position;
public StudentIterator(List<Student> students){
this.students=students;
this.position=0;
}
@Override
public boolean hasNext() {
return position<students.size();
}
@Override
public Object next() {
return students.get(position++);
}
}
import java.util.ArrayList;
import java.util.List;
/**
* @Description: 班级
* @Date: 2025-01-10 13:47
* @Author: gaoyufei
**/
public class StudentClass {
private List<Student> students=new ArrayList<>();
public void add(Student student){
this.students.add(student);
}
public void remove(Student student) {
students.remove(student);
}
public Iterator iterator(){
return new StudentIterator(students);
}
}
/**
* @Description: 客户端
* @Date: 2025-01-10 13:50
* @Author: gaoyufei
**/
public class Client {
public static void main(String[] args) {
StudentClass studentClass=new StudentClass();
studentClass.add(new Student("张三"));
studentClass.add(new Student("李四"));
studentClass.add(new Student("王五"));
studentClass.remove(new Student("王五"));
Iterator<Student> iterator = studentClass.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next().getName());
}
}
}