学习笔记 (设计模式)
第一章 Iterator 迭代器
Iterator是指依顺序遍历多个数字或变量。就像数组for循环遍历一样遍历数据
先看程序实例:
把书籍(book)放到书架(BookShelf)上并依次输出书名:
1)首先定义一个Aggravate接口,实行聚合,相当与数组多个数的聚合。
public interface Aggregate
{
public abstract Iterator iterator();
}
//接口Iterator
public interface Iterator
{
public abstract boolean hasNext(); // 有无下一个元素
public abstract Object next(); //下一个元素
}
//类 Books
public class Book
{
private String name;
public Book(String name)
{
this.name = name;
}
}
//BookShelf类
// 该类要实现Aggregate接口 实现聚合
public class BookShelf implements Aggregate
{
private Book[] books;
private int last = 0;
public BookShelf(int maxSize)
{
books = new Book[maxSize];
}
public Book getBookAt(int index)
{
return books[index];
}
publci void addBook(Book book)
{
this.book[last] = book;
last++;
}
public Iterator iterate()
{
return new BookShelfIterator(this);
}
}
//3) BookShelfIterator 类
public class BookShelfIterator implements Iterator
{
private BookShelf bookShelf;
private int index;
public BookShelfIterator(BookShelf bookShelf)
{
this.bkkoShelf = 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;
}
}
//实现完成 测试程序:
public class Main
{
public static void main(String[] args)
{
BookShelf bookShelf = new BookShelf(4);
bookShelf.addBook(new Book(“book “1”));
bookShelf.addBook(new Book(“book “2”));
bookShelf.addBook(new Book(“book “3”))
bookShelf.addBook(new Book(“book “4”));
Iterator ite = bookShelf.iterator();
while(ite.hasNext)
{
Book book =(Book)bookShelf.next();
System.out.println(book.getName());
}
}
}
为什么不使用数组直接遍历而用迭代器呢?
最大的理由是因为Iterator 可以实现分开 单独进行递增。
设计Pattern就是提高类的复用率,提高复用率是把类当作一部分零件来用,当修改是只要修改一部分零件,而不要修改其他零件。
( jdk Iterator 源码 ;
public interface Iterator<E> {
/**
* Returns <tt>true</tt> if the iteration has more elements. (In other
* words, returns <tt>true</tt> if <tt>next</tt> would return an element
* rather than throwing an exception.)
*
* @return <tt>true</tt> if the iterator has more elements.
*/
boolean hasNext();
/**
* Returns the next element in the iteration.
*
* @return the next element in the iteration.
* @exception NoSuchElementException iteration has no more elements.
*/
E next();
/**
*
* Removes from the underlying collection the last element returned by the
* iterator (optional operation). This method can be called only once per
* call to <tt>next</tt>. The behavior of an iterator is unspecified if
* the underlying collection is modified while the iteration is in
* progress in any way other than by calling this method.
*
* @exception UnsupportedOperationException if the <tt>remove</tt>
* operation is not supported by this Iterator.
* @exception IllegalStateException if the <tt>next</tt> method has not
* yet been called, or the <tt>remove</tt> method has already
* been called after the last call to the <tt>next</tt>
* method.
*/
void remove();
}
一个实现类
private class ListItr implements ListIterator<E> {
private Entry<E> lastReturned = header;
private Entry<E> next;
private int nextIndex;
private int expectedModCount = modCount;
ListItr(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
if (index < (size >> 1)) {
next = header.next;
for (nextIndex=0; nextIndex<index; nextIndex++)
next = next.next;
} else {
next = header;
for (nextIndex=size; nextIndex>index; nextIndex--)
next = next.previous;
}
}
public boolean hasNext() {
return nextIndex != size;
}
public E next() {
checkForComodification();
if (nextIndex == size)
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.element;
}
)