设计模式 --迭代器模式(Iterator)
能够游走于聚合内的每一个元素,同时还可以提供多种不同的遍历方式。
基本概念:
使用迭代器模式的优点:
- 遍历集合或者数组;
- 忽略集合和数组的结构;
- 提供不同的遍历方式;
- 符合单一职责原则。
迭代器角色:
- 抽象迭代器:该接口必须定义实现迭代功能的最小定义方法集。
- 具体迭代器:迭代器接口Iterator的实现类。可以根据具体情况加以实现。
- 抽象聚合类:定义基本功能以及提供类似Iterator iterator()的方法。
- 具体聚合类:容器接口的实现类。必须实现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=010204,name=Java从入门到精通,price60.0
使用场景:
设计模式 --迭代器模式(Iterator)的更多相关文章
- 设计模式 - 迭代器模式(iterator pattern) 具体解释
迭代器模式(iterator pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 迭代器模式(iterator pattern) : 提供一 ...
- javascript设计模式-迭代器模式(Iterator)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C#设计模式——迭代器模式(Iterator Pattern)
一.概述在软件开发过程中,我们可能会希望在不暴露一个集合对象内部结构的同时,可以让外部代码透明地访问其中包含的元素.迭代器模式可以解决这一问题.二.迭代器模式迭代器模式提供一种方法顺序访问一个集合对象 ...
- 设计模式 -- 迭代器模式(Iterator)
--------------------------------------------------------------------- 1.场景问题 考虑这样一个问题: 9个学生对象分别通过数组存 ...
- 设计模式(十):从电影院中认识"迭代器模式"(Iterator Pattern)
上篇博客我们从醋溜土豆丝与清炒苦瓜中认识了“模板方法模式”,那么在今天这篇博客中我们要从电影院中来认识"迭代器模式"(Iterator Pattern).“迭代器模式”顾名思义就是 ...
- 深入浅出设计模式——迭代器模式(Iterator Pattern)
模式动机 一个聚合对象,如一个列表(List)或者一个集合(Set),应该提供一种方法来让别人可以访问它的元素,而又不需要暴露它的内部结构.针对不同的需要,可能还要以不同的方式遍历整个聚合对象,但是我 ...
- 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)
设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型) 1.概述 类中的面向对象编程封装应用逻辑.类,就是实例化的对象,每个单独的对象都有一个特定的身份和状态.单独的对象是一种组织代码的 ...
- 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)
原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...
- 迭代器模式 Iterator 行为型 设计模式(二十)
迭代器模式(Iterator) 走遍天下,世界那么大,我想去看看 在计算机中,Iterator意为迭代器,迭代有重复的含义,在程序中,更有“遍历”的含义 如果给定一个数组,我们可以通过for循 ...
- Java 设计模式系列(十五)迭代器模式(Iterator)
Java 设计模式系列(十五)迭代器模式(Iterator) 迭代器模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象(interna ...
随机推荐
- 结构-行为-样式-JqueryUI拖放使用实例(全)
最近工作中有个需要是动态配置页面,想到之前公司有做过类似的,用的是JqueryUi,所以就看了下它的Api.下面就是我做的小Demo,想用的同学可以参考: Html: <div class=&q ...
- iOS 加载Image的两种方式
Apple官方文档对于加载image提供了两个方法 + (nullable UIImage *)imageNamed:(NSString *)name; + (nullable UIImage *)i ...
- Object转换为字符并去空格
<div id="txt" style="display:none">1."不积跬步,无以至千里"的古语说明( A ) A.没有 ...
- ubuntu16.04 samba 配置
samba是一个很有用的在Linux和Windows之间共享文件的服务器程序,在工作的时候一直在使用,不过都是别人配置好的环境,自已一直没有配置过Samba服务器,今天尝试着自己配置的一次遇到了很多的 ...
- 微信js-sdk调用
之前在做微信的时候,在微信支付还有调起微信扫一扫的时候,用过js-sdk.最近,被几个做前端的同学问到了具体的流程,想想,还是写下来好点. 微信js-sdk,是微信提供给网页开发设计者使用的, ...
- Gentoo/Arch常用软件配置
## Desktop Environment ### GNOME ### KDE ### LXDE ### Xfce ## Window Managers ### dwm x11-wm/dwm ### ...
- 命令行解决mysql中文乱码
修改my.ini文件中的 [mysql] default-character-set=gbk [mysqld] # The default character set that will be use ...
- nvl isnull coalesce
oracle 的NVL(col,0)是判断如果col字段为空的时候赋值0. postgresql里也有类似的方法 SELECT coalesce(collect_result,0) as collec ...
- 安装Genymotion与集成eclipse,最后有集成android studio
本安装过程从不用到VPN 一切国内网络都可以解决. 首先下载Genymotion,网址 https://www.genymotion.com/account/login/ 首先需要注册,我使用163 ...
- hibernate子查询
对于支持子查询的数据库,Hibernate支持在查询中使用子查询.一个子查询必须被圆括号包围起来(经常是SQL聚集函数的圆括号). 甚至相互关联的子查询(引用到外部查询中的别名的子查询)也是允许的. ...