Iterator模式 --一个一个遍历

在Java中的for语句中 i++的作用是让 i 的值在每次循环后自增1,这样就可以访问数组中的下一个元素、下下一个元素、再下下一个元素,也就实现了从头至尾逐一遍历数组元素的功能。

将这里的循环变量 i的作用抽象化、通用化后形成的模式,在设计模式中称为 Iterator 模式

示例程序

  • Aggregate接口

    Aggregate接口是索要遍历的集合的接口。实现了该接口的类将称为一个可以保持多个元素的集合。

public interface Aggregate {

     public abstract Iterator iterator();
}

Aggregate接口中的iterator 方法将会生成一个用于遍历集合的迭代器。想要遍历集合中的元素的时候可以调用该方法来生成一个实现了Iterator接口的类的实例。

  • Iterator接口
public interface Iterator {
public abstract boolean hasNext();
public abstract Object next();
}

  hasNext()方法主要用于循环终止条件。

  next()方法用于返回集合中的一个元素,并且为了在下次调用next方法时正确地反回下一个元素,在该方法中还需将迭代器移动至下一个元素的处理。

  • Book类
public class Book {
private String name;
public Book(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
  • BookShelf类
public class BookShelf implements Aggregate {
private Book[] books;
private int last = 0;
public BookShelf(int maxsize) {
this.books = new Book (maxSize);
}
public Book getBookAt(int index){
return books[index];
}
public void appendBook(Book book) {
this.books[last] = book'
last++;
}
public int getLength() {
return last;
} public Itrator iterator() {
return new BookShelfIterator(this);
}
}

BookShelf类是用来表示书架的类,将该类作为Book的集合进行处理,实现了Aggregate接口。

  • BookShelfIterator类
public class BookShelfIterator implements Iterator {
private BookShelf bookShelf;
private int index;
public BookShelfIterator(BookShelf bookShelf) {
this.bookShelf = bookShelf;
this.index = 0;
}
public boolean hasNext(){
if(index < bookShelf.getLength()){
return true;
} else {
return false;
}
}
public Object next(){
Book book = bookShelf.getBookAt(index);
index++;
return book;
}
}

BookShelfIterator类是一个迭代器的实现,它持有一个将要遍历的集合BookShelf书架。

  • Main类
public class Main{
public static void main(String[] args) {
BookShelf bookShelf = new BookShelf(4);
bookShelf.appendBook(new Book("倚天屠龙记"));
bookShelf.appendBook(new Book("葵花宝典"));
bookShelf.appendBook(new Book("九阳真经"));
bookShelf.appendBook(new Book("神雕侠侣"));
Iterator it = bookShelf.iterator();
while (it.hasNext()){
Book book = (Book) it.next();
System.out.println(book.getName());
}
}
}

总结

  • 为何要使用Iterator

引入Iterator后可以将遍历与实现分离开来,在遍历时只需调用迭代器的方法,而不用关心具体集合实现类的方法。假设以后需要对集合类的实现方式进行修改,只要集合中的Iterator方法能正确的返回Iterator实例,即使不对迭代器的使用者进行修改,遍历代码都能正常工作。

设计模式的作用就是帮助我们编写可复用的类。“可复用”就是将类实现为“组件”,当一个组件发生改变时,不需要对其他的组件进行修改或只需很小的修改即可应对。

  • 多个Iterator

将遍历功能置于Aggregate角色之外,是Iterator模式的一个特征。根据这几个特征,可以针对一个具体的ConcreateAggregate角色编写多个ConcreteIterator角色。

  • 对JAVA集合进行遍历删除时务必要用迭代器。
 private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0;
/**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1;
/**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}

ArrayList中的迭代器实现

【设计模式大法】Iterator模式的更多相关文章

  1. 设计模式之Iterator模式

    STL里的iterator就是应用了iterator模式. 一.什么是迭代模式 Iterator模式也叫迭代模式,是行为模式之一,它把对容器中包含的内部对象的访问委让给外部类,使用Iterator按顺 ...

  2. Java设计模式之Iterator模式

    分类: [java]2013-07-15 10:58 917人阅读 评论(0) 收藏 举报 所谓Iterator模式,即是Iterator为不同的容器提供一个统一的访问方式.本文以java中的容器为例 ...

  3. 设计模式—迭代器Iterator模式

    什么是迭代器模式? 让用户通过特定的接口访问容器的数据,不需要了解容器内部的数据结构. 首先我们先模仿集合中ArrayList和LinkedList的实现.一个是基于数组的实现.一个是基于链表的实现, ...

  4. 设计模式——迭代器(Iterator)模式

    概述 迭代器模式简单的说(按我目前的理解)就是一个类提供一个对外迭代的接口,方面调用者迭代.这个迭代接口至少包括两个方法:hasNext()--用于判断是否还有下一个,next()--用于取出下一个对 ...

  5. 设计模式之Iterator模式(2)

    这篇文章比较简单,作一个笔记. 模拟Iterator. Iterator接口: package cn.asto.Interator; public interface Iterator { publi ...

  6. 设计模式:Iterator模式

    目的:将数据的存储和数据的查询分开,降低数据的耦合性 继承关系图: 例子: //定义迭代器接口 template<typename T> class Iterator { public: ...

  7. 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)

    原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...

  8. Java设计模式(12)迭代模式(Iterator模式)

    上了这么多年学,我发现一个问题,好象老师都很喜欢点名,甚至点名都成了某些老师的嗜好,一日不点名,就饭吃不香,觉睡不好似的,我就觉得很奇怪,你的课要是讲的好,同学又怎么会不来听课呢,殊不知:“误人子弟, ...

  9. 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)

    设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...

随机推荐

  1. SpringBoot与热部署整合(五)

    一 Idea pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifa ...

  2. 2018.8.8 python 初识函数

    主要内容: 1.函数定义 2.函数名,函数体以及函数的调用 3.函数的返回值 4.函数的参数 一.函数的定义 函数:对代码块和功能的封装和定义. 二.函数名.函数体及函数的调用 我们使用def关键字来 ...

  3. Just For Test

    Just For Test

  4. Tomcat项目部署

    一 之前一直是在ecplise 利用tomcat插件的形式启动项目,这里可以通过选择server.xml和context files两种方式这里选择这两者方式,都是会在tomcat/bin下产生对应的 ...

  5. RTKLib的Manual解读

    Key-word: integer ambiguity resolution :整周模糊度解算 navigation:导航 Kinematic:动态,RTK的K rover:漫游 validation ...

  6. Linux JDK 安装与配置

    一.下载 JDK 官网链接:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 系 ...

  7. re模块的基本使用

    目录 re模块 常用元字符 特殊构造 贪婪模式 非贪婪模式 re的常用函数 re模块补充 关于re模块必须知道的知识点 re模块 re模块 , 即正则表达式 , 本身是一种小型的.高度专业化的编程语言 ...

  8. C语言I博客作业08

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 作业 我在这个课程的目标是 熟悉使用while和do-while结构解决问题 这个作业在那个具体方面帮助我实现目标 pta题目及查找的资料 ...

  9. Java类/接口的API

    本章节收集的类/接口API有: Object类,枚举,包装类,接口Comparable,类Arrays,异常, Object类 public String toString(): [把一个对象的信息用 ...

  10. spark基于yarn的两种提交模式

    一.spark的三种提交模式 1.第一种,Spark内核架构,即standalone模式,基于Spark自己的Master-Worker集群. 2.第二种,基于YARN的yarn-cluster模式. ...