设计模式之迭代器 Iterator

 

                                                                 表1.1  类,接口一览表

名称 说明 Aggregate 表示已聚合的接口 Iterator 执行递增,遍历的接口 Book 表示书籍的类 BookShelf 表示书架的类 BookShelfIterator 扫描书架的类 Main 测试用类   这个程序是把书籍(Book)放到书架(BookShelf)上,并依次输出书名。

 

 

 

 

 

  Aggregate 接口( Aggregate.java <o:p></o:p>
  1. public interface Aggregate {      
  2.   public abstract Iterator iterator();      
  3.  }    

Iterator 接口(Iterator.java<o:p></o:p>

  1. public interface Iterator {      
  2.    public abstract boolean hasNext();      
  3.    public abstract Object next();      
  4. }    
  Book 类( Book.java  <o:p></o:p>
  1. public class Book      
  2.  {      
  3.   private String name = "";      
  4.   public Book(String name) {      
  5.   this.name = name;      
  6.    }      
  7.    public String getName() {      
  8.    return name;      
  9.    }      
  10. }     

BookShelf类(BookShelf.java<o:p></o:p>

  1. public class BookShelf implements Aggregate {      
  2.   private Book[]  books;      
  3.   private int last = 0;      
  4.   public  BookShelf(int maxsize) {      
  5.      this.books = new  Book[maxsize];      
  6.    }      
  7.   public Book getBookAt(int index) {      
  8.     return books[index];      
  9.  }      
  10.  public  void appendBook(Book book) {      
  11.    this.books[last] = book ;      
  12.    last++;        
  13.   }      
  14.   public int getLength() {      
  15.    return new BookShelfIterator(this);      
  16.   }      
  17. }     
  18.   
BookShelfIterator类
  1. public class BookShelfIterator implaments Iterator {   
  2.   private BookShelf bookShelf;   
  3.   private int index;   
  4.   public BookShelfIterator (BookShelf boofShelf) {   
  5.      this.bookShelf = bookShelf;   
  6.      this.index = 0;   
  7.   }   
  8.   public  boolean hasNext () {   
  9.       if(index < bookShelf.getLength())  {   
  10.          return true;   
  11.        }else {   
  12.           return false;   
  13.        }   
  14.    }   
  15.    public  Object next () {   
  16.         Book book = bookShelf.getBookAt(index);   
  17.         index++;   
  18.          return book;   
  19.      }   
  20. }       
Main.java 测试类
  1. public class main{   
  2.   public static void main(String[] args) {   
  3.        BookShelf  bookShelf = new BookShelf(2);   
  4.        bookShelf.appendBook(new Book("Around  the  World  in 80 days"));   
  5.        bookShelf.appendBook(new Book("Bible"));   
  6.        Iterator it = bookShelf.iterator();   
  7.        while(it.hasNext()) {   
  8.           Book book = (Book)it.next();   
  9.           System.out.println(""+book.getName());   
  10.         }   
  11.   }   
  12. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值