美文网首页硬核空间技术博客
图解设计模式之Iterator模式

图解设计模式之Iterator模式

作者: 憨憨二师兄 | 来源:发表于2020-04-01 16:10 被阅读0次

示例程序

实现了Iterator模式的示例程序的作用是将书放置到书架中,并将书的名字顺序显示出来。



示例程序的UML图:



对应程序如下:

Book类

public class Book{
    private String name;
    public Book(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
}

Aggregate接口

public interface Aggregate{
    public abstract Iterator iterator();
}

Iterator接口

public interface Iterator{
    public abstract boolean hasNext();
    public abstract Object next();
}

BookShelf类

public class BookShelf implements Aggregate{
    public ArrayList<Book> books;
    
    public BookShelf(){
        this.books = new ArrayList<>();
    }
    public Book getBookAt(int index){
        return books.get(index);
    }
    
    public void appendBook(Book book){
        books.add(book);
    }
    
    public int getLength(){
        return books.size();
    }
    
    public Iterator iterator(){
        return new BookShelfIterator(this);
    }
}

BookShelfIterator类

public class BookShelfIterator implements Iterator{
    private BookShelf bookShelf;
    private int index;
    public BookShelfIterator(BookShelf bookShelf){
        this.bookShelf = bookShelf;
        this.index = 0;
    }
    
    public boolean hasNext(){
        return index < bookShelf.getLength();
    }
    
    public Object next(){
        Book book = bookShelf.getBookAt(index);
        index++;
        return book;
    }
}

Main类

public class Main{
    public static void main(String[] args){
        BookShelf bookShelf = new BookShelf();
        bookShelf.appendBook(new Book("西游记"));
        bookShelf.appendBook(new Book("水浒传"));
        bookShelf.appendBook(new Book("三国演义"));
        bookShelf.appendBook(new Book("红楼梦"));
        Iterator it = bookShelf.iterator();
        while(it.hasNext()){
            Book book = (Book)it.next();
            System.out.println(book.getName());
        }
    }
}

Iterator模式中参与角色

Iterator(迭代器)

该角色负责定义按照顺序遍历元素的接口。Iterator有两个方法:hasNext和next。hasNext方法用于判断是否存在下一个元素,next方法用于获取该元素。

ConcreteIterator(具体的迭代器)

该角色负责实现Iterator接口,在示例程序中由BookShelf扮演这一个角色。

Aggregate(集合)

该角色负责定义创建Iterator角色的接口。

ConcreteAggregate(具体的集合)

该角色负责实现Aggregate接口,它会创建具体的Iterator角色。程序示例中,BookShelf这个具体的集合扮演这个角色。

Iterator模式的类图

关于Iterator模式的思考

为什么要使用Iterator模式

在遍历时,如果使用Iterator遍历集合,那么我们只需要调用迭代器的hasNext和next方法,完全不用关心具体集合实现类的方法。
如果以后需要对集合的实现类中的方法进行修改,只要集合中的iterator方法能够正确返回Iterator的实例,那么main程序中,不需要修改任何代码,遍历就可以正常进行。在Java中,集合都是可遍历的,例如HashMap的遍历:

public class Main{
    public static void main(String[] args){
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"kim");
        map.put(2,"duby");
        map.put(3,"dobby");
        Iterator it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry entry = (Map.Entry) it.next();
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }
}

改进Iterator

在示例程序中,Iterator被设计成了只能从前向后遍历,其实,遍历的方法可以多种多样。
可以添加一些方法,比如:previous。这样既可以从前向后遍历也可以做到从后向前遍历集合了,关键在于你想让迭代器拥有什么样的遍历功能。

相关文章

网友评论

    本文标题:图解设计模式之Iterator模式

    本文链接:https://www.haomeiwen.com/subject/xexauhtx.html