迭代器模式
🍀参考书籍:图解设计模式,提取码5i9c
一.迭代器模式是什么?
迭代器模式的意图是提供一种方法顺序访问一个聚合对象中的各个元素,而不暴露该对象的内部表示。
迭代器模式属于行为型模式,其核心思想是通过定义一个迭代器接口,该接口包含访问和遍历元素的基本方法,如
next()
、hasNext()
、currentItem()
等。通过实现这个接口的具体迭代器类,可以维护对集合的引用,并跟踪当前遍历的位置,从而使得用户可以通过统一的接口来访问不同类型的聚合对象,而无需关心这些对象的内部结构。这种模式解决了不同聚合对象(如数组、链表、自定义结构等)需要不同遍历方式的问题,通过提供一个统一的迭代器接口,简化了对各种聚合对象的遍历操作。
迭代器模式实现类图:
二.实例
实例一:
创建两个接口:Aggregate和Iterator,和三个类:书,书架,书架迭代器。
关系如下UML图所示
Aggreate.java
public interface Aggregate {
public abstract Iterator iterator();
}
Book.java
public class Book {
private String name;
Book(String name){
this.name=name;
}
String getName() {
return name;
}
}
BookShelf.java
public class BookShelf implements Aggregate{
private Book Books[];
private int last=0;
BookShelf(int Maxsize){
Books=new Book[Maxsize];
}
Book getBookAt(int Index) {
return Books[Index];
}
void appendBook(Book book) {
Books[last++]=book;
}
int getlength() {
return last;
}
public Iterator iterator(){
return new BookShelfIterator(this);
}
}
BookShelfIterator.java
public class BookShelfIterator implements Iterator{
private BookShelf bookshelf;
private int index;
BookShelfIterator(BookShelf bookshelf){
this.bookshelf=bookshelf;
index=0;
}
public Boolean hasNext() {
if(bookshelf.getlength()>index) {
return true;
}
else return false;
}
@Override
public Object next() {
// TODO Auto-generated method stub
Book book=bookshelf.getBookAt(index);
index++;
return book;
}
}
Iterator.java
public interface Iterator {
abstract Object next();
abstract Boolean hasNext();
}
Main.java
public class Main {
public static void main(String []args) {
BookShelf bookshelf=new BookShelf(10);
bookshelf.appendBook(new Book("Jane Eyre"));
bookshelf.appendBook(new Book("Alive"));
bookshelf.appendBook(new Book("Shouting"));
bookshelf.appendBook(new Book("A Dream in Red Mansions"));
BookShelfIterator it=new BookShelfIterator(bookshelf);
while(it.hasNext()) {
Book book=(Book)it.next();
System.out.println(book.getName());
}
}
}
运行结果:
实例二:
增加一个比赛排名表的遍历,UML图如下,可以看出和书架的遍历是共享Aggregate和Iterator这两个接口的,这就是迭代器模式的好处。无论增加多少个需要遍历的聚合对象,无论它们的类型有多少种,只要实现两个接口就可以轻松实现遍历。结构清晰。
Main.java
public class Main {
public static void main(String []args) {
Competition_leaderbord competition_leaderbord=new Competition_leaderbord(10);
competition_leaderbord.appendCompetitor(new Competitor("XiaoWang",8));
competition_leaderbord.appendCompetitor(new Competitor("XiaoZhang",3));
competition_leaderbord.appendCompetitor(new Competitor("XiaoSong",6));
competition_leaderbord.appendCompetitor(new Competitor("XiaoWu",10));
leaderboardIterator it=(leaderboardIterator)competition_leaderbord.iterator();
while(it.hasNext()) {
Competitor competitor=(Competitor)it.next();
System.out.println(competitor.getname()+"名次是:"+competitor.getranking());
}
}
}
Competition_leaderbord .java
public class Competition_leaderbord implements Aggregate{
private Competitor competitiors[];
private int last=0;
Competition_leaderbord(int MaxSize){
competitiors=new Competitor[MaxSize];
}
Competitor getCompetitorAt(int index) {
return competitiors[index];
}
void appendCompetitor(Competitor competitor) {
competitiors[last++]=competitor;
}
int getLength() {
return last;
}
@Override
public Iterator iterator() {
// TODO Auto-generated method stub
return new leaderboardIterator(this);
}
}
Competitor.java
public class Competitor {
private String name;
private int ranking;
Competitor(String name, int ranking){
this.name=name;
this.ranking=ranking;
}
String getname() {
return name;
}
int getranking() {
return ranking;
}
}
leaderboardIterator.java
public class leaderboardIterator implements Iterator{
private Competition_leaderbord competition_leaderboard;
private int index=0;
leaderboardIterator(Competition_leaderbord competition_leaderboard){
this.competition_leaderboard=competition_leaderboard;
}
@Override
public Object next() {
// TODO Auto-generated method stub
Competitor competitor=competition_leaderboard.getCompetitorAt(index);
index++;
return competitor;
}
@Override
public Boolean hasNext() {
// TODO Auto-generated method stub
if(competition_leaderboard.getLength()>index)
return true;
else return false;
}
}
运行结果:
习题:
习题1
答:
import java.util.*;
public class BookShelf implements Aggregate{
private ArrayList<Book>Books;
BookShelf(int InitialSize){
Books=new ArrayList<Book>(InitialSize);
}
Book getBookAt(int Index) {
return Books.get(Index);
}
void appendBook(Book book) {
Books.add(book);
}
int getlength() {
return Books.size();
}
public Iterator iterator(){
return new BookShelfIterator(this);
}
}