设计模式入门读书笔记(1):Iterator 一个一个数一遍

本文介绍Java中的迭代器模式,详细解析其核心概念、角色及应用案例。通过具体代码示例展示了如何利用迭代器模式实现对集合类的遍历,同时讨论了该模式的优势及其与其他设计模式的关系。

1. Iterator模式

Java语言为了把arr数组的内容全部表示出来,使用for语句按照下面的方法书写。

 

for (int i = 0; i < arr.length; i++) {

    System.out.println(arr[i]);

}

 

这里通过变量i的递增来达到数组遍历。

我们把变量i的动作抽象化,一般化后的设计模式叫做迭代模式(Iterator Pattern)。

 

迭代模式,就是把某种对象大量集中到一起,按照顺序进行全体扫描处理的一种设计模式。

 

2.迭代模式的例子

 

迭代模式

 

 

 使用到的类和接口一览:

Aggregate: 表示集合的接口

Iterator:  执行遍历扫描的接口

Book:  表示书的类

BookShelf:  表示书架的类

BookShelfIterator: 遍历书架的类

Main: 动作测试用的类

 

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

 

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

 

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

 

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 books[index];
    }
    public void appendBook(Book book) {
        this.books[last] = book;
        last++;
    }
    public int getLength() {
        return last;
    }
    public Iterator iterator() {
        return new BookShelfIterator(this);
    }
}

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;
    }
}

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        BookShelf bookShelf = new BookShelf(4);
        bookShelf.appendBook(new Book("Around the World in 80 Days"));
        bookShelf.appendBook(new Book("Bible"));
        bookShelf.appendBook(new Book("Cinderella"));
        bookShelf.appendBook(new Book("Daddy-Long-Legs"));
        Iterator it = bookShelf.iterator();
        while (it.hasNext()) {
            Book book = (Book)it.next();
            System.out.println(book.getName());
        }
    }
}

 

3.迭代模式的登场角色

(1) Iterator的角色

 担当确定顺序扫描各个元素用的接口API。next取得下一个元素,hasNext判断下一个元素是否存在。

 

(2)ConcreteIterator(具体的循环子)的角色

BookShelfIterator类担当具体的实现类。

 

(3)Aggregate(集合)的角色

生成Iterator角色的接口API。

 

(4)ConcreteAggregate(具体的集合体)的角色

BookShelf担当具体的实现类。

 

4.扩展你思路的提示

(1)无论实作怎样,都可以使用Iterator。

  为什么要考虑Iterator模式这样麻烦的东西呢,for循环不是更直接吗。一个很大的理由是,通过使用Iterator,使程序和具体实现分离的情况下的遍历变得可能。不再依赖BookShelf的实作。

 

while (it.hasNext()) {

    Book book = (Book)it.next();

    System.out.println(book.getName());

}

 

(2)不擅长使用抽象类和接口

就算不使用Aggregate类,而直接使用具体类BookShelf,不也能实现吗。

是可以,但是,一旦使用了具体类,类之间的结合变得更加紧密,把各个类作为零件重复使用也变得困难。因此才导入抽象类和接口。

同理,也不直接使用BookShelfIterator。

当具体类发生变化的使用,调用它的代码基本上不用做修改。

每个具体的集合对应一个具体的迭代类,当集合类的实现发生变化是,仅需修改具体迭代类即可。

 

(3)『下一个』容易引起误会

其实next相当于  returnCurrentElementAndAdvanceToNextPosition,返回当前元素,并且把指针移向下一个元素。

 

(4)多个Iterator

一个具体集合类,可以有多个具体迭代类。

 

(5)迭代的种类很多

迭代的种类不限于顺序扫描。

  a)从最后尾开始逆向扫描

  b)顺序扫描,逆向扫描两个方向 next,privious

  c)指定元素索引,直接跳转

 

 

5.相关模式

(1)Visitor模式

  针对所有元素,循环做同样的处理。

 

(2)Composite模式

  递归的结构。

 

(3)Factory Method模式

  迭代子实例的做成,可以通过工厂方法来完成。

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值