溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

圖解設計模式之Iterator模式

發布時間:2020-07-24 11:20:11 來源:網絡 閱讀:646 作者:Adam的blog 欄目:開發技術

一:什么是Iterator模式?

將循環變量i的作用抽象化,通用化形成的模式,在設計模式中稱為Iterator模式(迭代器模式),該模式用于在數據集合中按照順序遍歷集合

二:為什么要有Iterator模式?

為了回答這個問題,我們先看示例程序:
?1.?示例程序的類圖
圖解設計模式之Iterator模式
?2.?類和接口的示意圖
圖解設計模式之Iterator模式
?3.?示例程序
??>1.?Aggregate接口

package com.zgz.dm.Iterator;
/**
 * 表示集合的接口
 * @author guozhenZhao
 * @date 2018年6月12日
 */
public interface Aggregate {
    //該方法生成一個用于遍歷集合的迭代器
    public abstract Iterator iterator();
}

??>2.?Iterator接口

package com.zgz.dm.Iterator;
/**
 * 該接口用于遍歷集合中的元素
 * @author guozhenZhao
 * @date 2018年6月12日
 */
public interface Iterator {
    //判斷集合中是否存在下一個元素
    public abstract boolean hasNext();
    //獲取集合中的下一個元素
    public abstract Object next();
}

??>3.?Book類

package com.zgz.dm.Iterator;
/**
 * 表示書這個類
 * @author guozhenZhao
 * @date 2018年6月12日
 */
public class Book {
    private String name;

    public String getName() {
        return name;
    }

    public Book(String name) {
        super();
        this.name = name;
    }

}

??>4.?BookShelf類

package com.zgz.dm.Iterator;
/** 
 * 表示書架的類
 * @author guozhenZhao
 * @date 2018年6月12日
 */

import java.util.ArrayList;
import java.util.List;

public class BookShelf implements Aggregate {
    //private Book[] books;
    //private int last = 0;
    private List<Book> books;

    public BookShelf() {
        super();
        this.books = new ArrayList<Book>();
    }

    //獲取書架中對應的書
    public Book getBookAt(int index) {
        //return books[index];
        return books.get(index);
    }

    //向書架中添加書
    public void appendBook(Book book) {
        //this.books[last] = book;
        //last++;
        this.books.add(book);
    }

    //獲取書架的長度
    public int getLength() {
        //return last;
        return books.size();
    }

    //遍歷書架中的書
    @Override
    public Iterator iterator() {
        // TODO Auto-generated method stub
        return new BookShelfIterator(this);
    }

}

??>5.?BookShelfIterator類

package com.zgz.dm.Iterator;
/**
 * 遍歷書架的類
 * @author guozhenZhao
 * @date 2018年6月12日
 */
public class BookShelfIterator implements Iterator{

    private BookShelf bookShelf;
    private int index;

    public BookShelfIterator(BookShelf bookShelf) {
        super();
        this.bookShelf = bookShelf;
        this.index = 0;
    }

    @Override
    public boolean hasNext() {
        // TODO Auto-generated method stub
        if (index < bookShelf.getLength()) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Object next() {
        // TODO Auto-generated method stub
        Book book = bookShelf.getBookAt(index);
        index++;
        return book;
    }

}

??>6.?測試類

package com.zgz.dm.Iterator;
/**
 * 測試類
 * @author guozhenZhao
 * @date 2018年6月12日
 */
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BookShelf bookShelf = new BookShelf();
        bookShelf.appendBook(new Book("追風箏的人"));
        bookShelf.appendBook(new Book("java編程思想"));
        bookShelf.appendBook(new Book("SSM整合"));
        bookShelf.appendBook(new Book("平凡的世界"));
        bookShelf.appendBook(new Book("springBoot"));

        Iterator it = bookShelf.iterator();
        while(it.hasNext()) {
            Book book = (Book)it.next();
            System.out.println(book.getName());
        }
    }

}

 讀完示例程序,回答上面的問題,為什么要有Iterator模式呢?如果是數組的話直接使用for循環遍歷不就得了。在上面的程序中有一個BookShelf類,其中一個方法,如下圖:
圖解設計模式之Iterator模式
在這個方法中返回的是Iterator并不是對應的對象類,在測試類中遍歷的時候,如下圖:
圖解設計模式之Iterator模式
上面的代碼調用的是Iterator的hasNext()方法和next方法,此時的while循環不依賴BookShelf類的實現。所以引入Iterator后可以將遍歷和實現分離開。設計模式的作用就是幫助我們編寫可以復用的類,所謂的可復用就是將類實現為一個組件,在需要變動時,便于修改,所以也就不難理解為啥上圖代碼返回的是Iterator類型了。學習設計模式,其思想在于:
  不要只使用具體類編程,優先使用抽象類和借口來編程

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女