Java设计模式—迭代器(Iterator)
2013-04-24 19:45
856 查看
Java设计模式—迭代器(Iterator)
转自(http://blog.163.com/nonoliuhao@126/blog/static/1716520942010101131346478/)1.Aggregate接口所声明的方法只有iterator方法一个,这是为了建立一个对对应聚合的 iterator
Java代码 package com.pattern.iterator;
publicinterface Aggregate {
publicabstract Iterator iterator();
}
package com.pattern.iterator; public interface Aggregate { public abstract Iterator iterator(); }2.iterator接口,执行元素递增,具有类似循环变量的功能。
Java代码 package com.pattern.iterator;
publicinterface Iterator {
publicabstractboolean hasNext();
publicabstract Object next();
}
package com.pattern.iterator; public interface Iterator { public abstract boolean hasNext(); public abstract Object next(); }3. 书籍类
Java代码 package com.pattern.iterator;
publicclass Book {
private String name="";
public Book(String name) {
this.name = name;
}
/**
* 获得书籍名称
* @return String
*/
public String getName() {
return name;
}
}
package com.pattern.iterator; public class Book { private String name=""; public Book(String name) { this.name = name; } /** * 获得书籍名称 * @return String */ public String getName() { return name; } }4.书架类
Java代码 package com.pattern.iterator;
/**
* 书架类
* @author administrator
*/
publicclass BookShelf implements Aggregate{
private Book[] books;
privateint last = 0;
public BookShelf(int maxSize) {
this.books = new Book[maxSize];
}
public Book getBookAt(int index) {
return books[index];
}
//添加书籍
publicvoid appendBook(Book book) {
this.books[last] = book;
last++;
}
//获得书架存书的数量
publicint getLength() {
return books.length;
}
//获得书架迭代器对象
public Iterator iterator() {
returnnew BookShelfIterator(this);
}
}
package com.pattern.iterator; /** * 书架类 * @author administrator */ 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 books.length; } //获得书架迭代器对象 public Iterator iterator() { return new BookShelfIterator(this); } }5.书架迭代器类
Java代码 package com.pattern.iterator;
publicclass BookShelfIterator implements Iterator{
private BookShelf bookShelf;
privateint index;
public BookShelfIterator(BookShelf bookShelf) {
this.bookShelf = bookShelf;
this.index = 0;
}
//检查是否还有下一本书
publicboolean hasNext() {
if(index < bookShelf.getLength()) {
returntrue;
}
else {
returnfalse;
}
}
//返回指定位置的书籍
public Object next() {
Book book = bookShelf.getBookAt(index);
index ++;
return book;
}
}
package com.pattern.iterator; 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; } }6.测试类
Java代码 package com.pattern.iterator;
publicclass Main {
publicstaticvoid main(String[] args) {
//生成一个书架
BookShelf bookShelf = new BookShelf(4);
//向书架添加书籍
bookShelf.appendBook(new Book("周恩来的晚年岁月"));
bookShelf.appendBook(new Book("C++网络编程"));
bookShelf.appendBook(new Book("J2EE网络编程精解"));
bookShelf.appendBook(new Book("Java编程思想"));
//获得书架迭代器
Iterator it = bookShelf.iterator();
while(it.hasNext()) {
Book book = (Book)it.next();
System.out.println(book.getName());
}
}
}
package com.pattern.iterator; public class Main { public static void main(String[] args) { //生成一个书架 BookShelf bookShelf = new BookShelf(4); //向书架添加书籍 bookShelf.appendBook(new Book("周恩来的晚年岁月")); bookShelf.appendBook(new Book("C++网络编程")); bookShelf.appendBook(new Book("J2EE网络编程精解")); bookShelf.appendBook(new Book("Java编程思想")); //获得书架迭代器 Iterator it = bookShelf.iterator(); while(it.hasNext()) { Book book = (Book)it.next(); System.out.println(book.getName()); } } }输出结果:
周恩来的晚年岁月
C++网络编程 J2EE网络编程精解 Java编程思想
迭代器设计恩想: 迭代器可以顺序访问一个聚集中的元素而不必显露聚集的内部对象。多个对象聚在一起形成的总体称为聚集,聚集对象是能够包容一组对象的容器对象。迭代器模式将迭代逻辑封装到一个独立的对象中,从而与聚集本身隔开。迭代算法独立于聚集对象,修改迭代算法不会对聚集对象产生任何影响,实现程序的松耦合。
相关文章推荐
- Java设计模式—迭代器(Iterator)
- 设计模式 - 迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释
- Java设计模式—迭代器(Iterator)
- 设计模式 - 迭代器模式(iterator pattern) Java 迭代器(Iterator) 详解
- java设计模式之迭代器 Iterator
- Java设计模式—迭代器(Iterator)
- Java设计模式之从[魔兽争霸、星际争霸、DOTA编队]分析迭代器(Iterator)模式
- Head First 设计模式 (九) 迭代器与组合模式(Iterator & Composite pattern) C++实现
- [016]Java设计模式2——Iterator
- Java设计模式-迭代子模式(Iterator)
- java设计模式-迭代器模式(Iterator)
- java设计模式之Iterator
- [译]Java 设计模式之迭代器
- java设计模式(三)——模板、访问者、组合、解释器、迭代器、中介者
- JS-Javascript 迭代器设计模式 Iterator pattern
- 设计模式之迭代器与组合模式(java)
- Java设计模式(18)——行为模式之迭代子模式(Iterator)
- Java23种设计模式:Iterator设计模式(三)
- 设计模式之iterator模式到STL中iterator迭代器
- 【设计模式基础】行为模式 - 7 - 迭代器(Iterator)