设计模式 Iterator

                    学习笔记 (设计模式)

第一章                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;
	}



 

 

 

)
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值