能够游走于聚合内的每一个元素,同时还可以提供多种不同的遍历方式。

 

基本概念:

就是提供一种方法顺序访问一个聚合对象中的各个元素,而不是暴露其内部的表示。
 

使用迭代器模式的优点:

  1. 遍历集合或者数组;
  2. 忽略集合和数组的结构;
  3. 提供不同的遍历方式;
  4. 符合单一职责原则。

迭代器角色:

    1. 抽象迭代器:该接口必须定义实现迭代功能的最小定义方法集。
    2. 具体迭代器:迭代器接口Iterator的实现类。可以根据具体情况加以实现。
    3. 抽象聚合类:定义基本功能以及提供类似Iterator iterator()的方法。
    4. 具体聚合类:容器接口的实现类。必须实现Iterator iterator()方法。

Book.java

package com.soyoungboy.iterator2;
public class Book {
private String ISBN;
private String name;
private double price; public Book(String isbn, String name, double price) {
ISBN = isbn;
this.name = name;
this.price = price;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String isbn) {
ISBN = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
} public void display() {
System.out.println("ISBN=" + ISBN + ",name=" + name + ",price" + price);
} }

抽象迭代器Iterator.java

public interface Iterator {
boolean hasNext();
Object next();
void remove();
}

具体迭代器Itr 具体聚合类BookList.java

package com.soyoungboy.iterator2;
import java.util.ArrayList;
import java.util.List;
public class BookList {
private List<Book> bookList;
private int index;
public BookList() {
bookList = new ArrayList<Book>();
} //添加书籍
public void addBook(Book book) {
bookList.add(book);
} //删除书籍
public void deleteBook(Book book) {
int bookIndex = bookList.indexOf(book);
bookList.remove(bookIndex);
} public Iterator Iterator() {
return new Itr();
} /**
* 具体迭代器
* @author soyoungboy
*
*/
private class Itr implements Iterator{
public boolean hasNext() {
if(index >= bookList.size()) {
return false;
}
return true;
}
public Object next() {
return bookList.get(index++);
}
public void remove() { } } }

测试代码类:MainClass.java

package com.soyoungboy.iterator2;
public class MainClss {
public static void main(String[] args) {
BookList bookList = new BookList();
Book book1 = new Book("010203", "Java编程思想", 90);
Book book2 = new Book("010204", "Java从入门到精通", 60);
bookList.addBook(book1);
bookList.addBook(book2);
Iterator iter = bookList.Iterator();
while (iter.hasNext()) {
Book book = (Book) iter.next();
book.display();
}
}
}
运行结果:
 
ISBN=010203,name=Java编程思想,price90.0

ISBN=010204,name=Java从入门到精通,price60.0

使用场景:

java中数据集合遍历通常都会使用到迭代器设计模式进行数据的遍历过程;
部分源码可参考:http://www.cnblogs.com/wjun530/archive/2007/06/11/778709.html这篇博文进行分析
如通过Hashtable.elements方法可以得到一个Enumeration,然后通过这个Enumeration访问Hashtable中的数据,而不用关心Hashtable中的数据存放方式。
如果有如下业务需求也可使用迭代器设计模式:
访问容器中包含的内部对象;
按顺序访问。
 
参考文献资料:
http://blog.sina.com.cn/s/blog_6e5e78bf0101owrq.html

设计模式 --迭代器模式(Iterator)的更多相关文章

  1. 设计模式 - 迭代器模式(iterator pattern) 具体解释

    迭代器模式(iterator pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 迭代器模式(iterator pattern) : 提供一 ...

  2. javascript设计模式-迭代器模式(Iterator)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. C#设计模式——迭代器模式(Iterator Pattern)

    一.概述在软件开发过程中,我们可能会希望在不暴露一个集合对象内部结构的同时,可以让外部代码透明地访问其中包含的元素.迭代器模式可以解决这一问题.二.迭代器模式迭代器模式提供一种方法顺序访问一个集合对象 ...

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

    --------------------------------------------------------------------- 1.场景问题 考虑这样一个问题: 9个学生对象分别通过数组存 ...

  5. 设计模式(十):从电影院中认识"迭代器模式"(Iterator Pattern)

    上篇博客我们从醋溜土豆丝与清炒苦瓜中认识了“模板方法模式”,那么在今天这篇博客中我们要从电影院中来认识"迭代器模式"(Iterator Pattern).“迭代器模式”顾名思义就是 ...

  6. 深入浅出设计模式——迭代器模式(Iterator Pattern)

    模式动机 一个聚合对象,如一个列表(List)或者一个集合(Set),应该提供一种方法来让别人可以访问它的元素,而又不需要暴露它的内部结构.针对不同的需要,可能还要以不同的方式遍历整个聚合对象,但是我 ...

  7. 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)

      设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型) 1.概述 类中的面向对象编程封装应用逻辑.类,就是实例化的对象,每个单独的对象都有一个特定的身份和状态.单独的对象是一种组织代码的 ...

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

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

  9. 迭代器模式 Iterator 行为型 设计模式(二十)

    迭代器模式(Iterator)   走遍天下,世界那么大,我想去看看   在计算机中,Iterator意为迭代器,迭代有重复的含义,在程序中,更有“遍历”的含义 如果给定一个数组,我们可以通过for循 ...

  10. Java 设计模式系列(十五)迭代器模式(Iterator)

    Java 设计模式系列(十五)迭代器模式(Iterator) 迭代器模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象(interna ...

随机推荐

  1. Microsoft Edge与Google Chrome那些不同的举止

    以下针对14393版本Edge与Chrome 54 html dom/select的如果options里没有符合的值时edge会选择第一个,chrome(54)会置空选项 html dom/input ...

  2. Unity Get Thread Content Failed

    最近在使用Unity做项目时,发现总是莫名的出现“Get Thread Content Failed”的消息弹出,然后Unity就卡死了,这样反反复复,后来查到是因为一些杀毒软件在阻止Unity,尝试 ...

  3. Docker集群实验环境布署--swarm【4 管理组件--manager】

    主机分配如下,支持双活,中断其中1台,primary会通过consul自动重新选举   10.40.100.141 docker-manager0.venic.com 10.40.100.142 do ...

  4. bootstrp-select插件使用

    需要导入 <link rel="stylesheet" href="js/plugins/silviomoreto-bootstrap-select20151109 ...

  5. mysql解压版的配置安装

    先在CMD进入编辑筐,用管理员身份运行 切换到mysql的解压目录的bin目录下并输入mysqld -install 这个时候启动服务, 发现出错!!! 检查这两个文件 这里的路径一定要核对 再次启动 ...

  6. 常见IT面试题

    1.爬楼梯问题,有个N阶的楼梯,一个人可以一次爬1阶,也可以爬2阶,求问总计有多少种爬法? F(N)= F(N-1)+F(N-2). N=1时,有1种爬法,N=2时,有2种爬法.该题可以用递归方法求解 ...

  7. centos搭建svn服务器

    1.在centos6.5上面搭建svn服务器,安装svn服务器:yum install subversion 2.在任意目录下创建仓库目录,这里放在/data/mypros目录下 3.执行命令:svn ...

  8. 四位len灯流水

    #include <msp430x14x.h> //#include<intrins.h> #define uint unsigned int void delay(long ...

  9. 分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群(转载-2)

    原文:http://www.cnblogs.com/PurpleDream/p/4510279.html 分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群 前言:       ...

  10. Android如何查看应用签名信息

    转自http://www.trinea.cn/android/android-view-signatures/comment-page-1/ 介绍Android如何查看自己的应用签名及三方APK或系统 ...