"Graphic design patterns," the study notes 1-1 Iterator mode

FIG iterator class model

Explained in FIG class

name Explanation
Aggregate Collection interfaces, there is provided a method iterator
Iterator Iterator interface provides iterative operations
BookShelf Bookshelf class
Book Book class
BookShelfIterator Iterator class bookshelf

Iterative mode code

//集合接口
public interface Aggregate {
    //生成迭代器的方法
    public abstract Iterator iterator();
}

//迭代器接口
public interface Iterator {
    public abstract boolean hasNext();
    public abstract Object next();
}

//书架类
public class BookShelf implements Aggregate {
    private Book[] books;
    private int last = 0;
    public BookShelf(int maxSize) {
        this.books = new Book[maxSize];
    }
    public Book getBookAt(int index) {
        return this.books[index];
    }
    public void appendBook(Book book) {
        this.books[last] = book;
        last++;
    }
    public int getLength() {
        return last;
    }
    //获得BookShelf迭代器
    public Iterator iterator() {
        return new BookShelfIterator(this);
    }
}

//书类
public class Book {
    private String name;
    public Book(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

//书架迭代器类
public class BookShelfIterator implements Iterator {
    private BookShelf bookShelf;
    private int index;
    public BookShelfIterator(BookShelf bookShelf) {
        this.bookShelf = 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 static void main(String[] args) {
        BookShelf bookShelf = new BookShelf(4);
        bookShelf.appendBook(new Book("西游记"));
        bookShelf.appendBook(new Book("红楼梦"));
        bookShelf.appendBook(new Book("水浒传"));
        bookShelf.appendBook(new Book("三国演义"));

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

Explanation

  • BookShelf corresponds to a set of Book, equivalent Aggregate Interface Toolkit "handling tool set" of, BookShelf achieved Aggregate interfaces, may be removed from the toolbox set inside handling tool.

  • Iterator traversal is a collection of tools, BookShelfIterator implement Iterator interface, you have the ability to loop through the collection, through specialization modify, become a tool of special traverse BookShelf.

  • In this way, we can achieve Book collection traversal tool BookShelfIterator, use it to traverse the BookShelf from BookShelf in.

the reason

  • Since it is through a collection, a for loop does not write directly on the line, why should such trouble?

  • Because: collection can be an array, it can be a Vector, also can be a list, each traversal methods are not the same. If you want to use the array into list, then the whole set of code had to be rewritten. After using the iterative mode, can be realized only by changing the particular iterative method.

thought

  • The general and specific methods of separation.

  • Weakening the coupling between the classes, so that assembly is easier as a class are multiplexed.

  • Try to avoid using a particular class program, preferably used abstract classes and interfaces.

  • To put it plainly, do not write code written in too specific, some abstract ... to change the Ye Hao change.

Guess you like

Origin www.cnblogs.com/qianbixin/p/10992670.html