迭代器模式(Iterator)(常用)

  • 迭代器模式提供一種方法順序訪問一個聚合對象中的各個元素,而又不暴露其內部的表示。
  • 迭代器模式讓我們能游走于聚合內的每一個元素,而又不暴露其內部的表示。
  • 把游走的任務放在迭代器上,而不是聚合上。這樣簡化了聚合的接口和實現(xiàn),也讓責任各得其所。
  • 該模式依賴于Iterator接口
  • 將元素的遍歷和實現(xiàn)分離開來

- 主要角色

  • 抽象聚合(Aggregate)角色:定義存儲、添加、刪除聚合對象以及創(chuàng)建迭代器對象的接口。
  • 具體聚合(ConcreteAggregate)角色:實現(xiàn)抽象聚合類,返回一個具體迭代器的實例。
  • 抽象迭代器(Iterator)角色:定義訪問和遍歷聚合元素的接口,通常包含 hasNext()、first()、next() 等方法。
  • 具體迭代器(Concretelterator)角色:實現(xiàn)抽象迭代器接口中所定義的方法,完成對聚合對象的遍歷,記錄遍歷的當前位置。

- 結構圖

diedai.gif
public class ChineseBook {
      private String name;
  
      public ChineseBook(String name) {
          this.name = name;
      }
  
     public String getName() {
         return name;
     }
 }
public interface Aggregate {
       public abstract Iterator iterator();
 }
 public class ChineseBookShelf implements Aggregate {
       private ChineseBook[] books;
       int pointer=0;
       public ChineseBookShelf(int max_size){
         books=new ChineseBook[max_size];
       }
       public void appendChineseBook(ChineseBook book){
         books[pointer]=book;
         pointer++;
       }
       public ChineseBook findChineseBookAt(int index){
          return books[index];
       }
       public int getLength(){
            return pointer;
       }
       public Iterator iterator(){
          return new ChineseBookShelfIterator(this);
       }
 }

關鍵點

 public interface Iterator {
   public abstract boolean hasNext();
   public abstract Object  next();
 }
 public class ChineseBookShelfIterator implements Iterator {
  
     ChineseBookShelf bookShelf;
     int index;
     public ChineseBookShelfIterator(ChineseBookShelf bookShelf){
         this.bookShelf=bookShelf;
         index=0;
     }
     public boolean hasNext() {
         if(index<this.bookShelf.getLength()){
             return true;
         }
         return false;
     }
 
     public Object next() {
         return bookShelf.findChineseBookAt(index++);
     }
 }
 public class Main {
 
     public static void main(String[] args) {
         
         ChineseBook book1 = new ChineseBook("朝花夕拾");
         ChineseBook book2 = new ChineseBook("圍城");
         ChineseBook book3 = new ChineseBook("遮天");
         ChineseBook book4 = new ChineseBook("尋秦記");
         ChineseBook book5 = new ChineseBook("駱駝祥子");
 
         ChineseBookShelf bookShelf = new ChineseBookShelf(5);
 
         bookShelf.appendBook(book1);
         bookShelf.appendBook(book2);
         bookShelf.appendBook(book3);
         bookShelf.appendBook(book4);
         bookShelf.appendBook(book5);
         
         Iterator it= bookShelf.iterator();
         while(it.hasNext()){
             ChineseBook book=(ChineseBook)it.next();
             System.out.println("書的名字為《"+book.getName()+"》");
         }
     }
 }

如果這個時候有另外一個書架,但是他的實現(xiàn)方式是arrayList與上面的數(shù)組不同,那個就可以編寫這個新書架的Iterator。如果要實現(xiàn)拿出兩個書架的書,就可以創(chuàng)建相應的迭代器,簡化獲取書時的代碼,也無需知道每個書架是怎么實現(xiàn)循環(huán)輸出的。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容