19迭代模式Iterator

一、什么是迭代模式

  Iterator模式也叫迭代模式,是行为模式之 一,它把对容器中包含的内部对象的访问委让给 外部类,使用Iterator(遍历)按顺序进行遍历 访问的设计模式。

二、不使用迭代模式的应用

  在应用Iterator模式之前,首先应该明白Iterator 模式用来解决什么问题。或者说,如果不使用 Iterator模式,会存在什么问题。

    1.由容器自己实现顺序遍历。直接在容器类里直接添加顺序遍历方法

    2.让调用者自己实现遍历。直接暴露数据细节给外部。

三、不使用迭代模式的缺点

  以上方法1与方法2都可以实现对遍历,这样有问、 题呢?

    1,容器类承担了太多功能:一方面需要提供添加删除等本身应有的功能;一方面还需要提供遍历访问功能。

    2,往往容器在实现遍历的过程中,需要保存遍历状态,当跟元素的添加删除等功能夹杂在一起,很容易引起混乱和程序运行错误等。

四、使用迭代模式的应用

  Iterator模式就是为了有效地处理按顺序进行遍历访问 的一种设计模式,简单地说,Iterator模式提供一种有效 的方法,可以屏蔽聚集对象集合的容器类的实现细节, 而能对容器内包含的对象元素按顺序进行有效的遍历访 问。

  所以,Iterator模式的应用场景可以归纳为满足以下几个 条件:

     - 访问容器中包含的内部对象

     - 按顺序访问

五、迭代模式的结构

五、迭代模式的角色和职责

  Iterator(迭代器接口): 该接口必须定义实现迭代功能的最小定义方法集 比如提供hasNext()和next()方法。

  ConcreteIterator(迭代器实现类): 迭代器接口Iterator的实现类。可以根据具体情况加以实现。

  Aggregate(容器接口): 定义基本功能以及提供类似Iterator iterator()的方法。

  concreteAggregate(容器实现类): 容器接口的实现类。必须实现Iterator iterator()方法。

六、迭代模式的优点

  1,实现功能分离,简化容器接口。让容器只实现本身的基本功能,把迭代功能委让给外部类实现,符合类的设计原则。

  2,隐藏容器的实现细节。

  3,为容器或其子容器提供了一个统一接口,一方面方便调用;另一方面使得调用者不必关注迭代器的实现细节。

  4,可以为容器或其子容器实现不同的迭代方法或多个迭代方法。

 1 public class Book {
 2     private String ISBN;
 3     private String name;
 4     private double price;
 5     
 6     public Book(String isbn, String name, double price) {
 7         ISBN = isbn;
 8         this.name = name;
 9         this.price = price;
10     }
11 
12     public String getISBN() {
13         return ISBN;
14     }
15 
16     public void setISBN(String isbn) {
17         ISBN = isbn;
18     }
19 
20     public String getName() {
21         return name;
22     }
23 
24     public void setName(String name) {
25         this.name = name;
26     }
27 
28     public double getPrice() {
29         return price;
30     }
31 
32     public void setPrice(double price) {
33         this.price = price;
34     }
35     
36     public void display() {
37         System.out.println("ISBN=" + ISBN + ",name=" + name + ",price" + price);
38     }    
39 }

图书列表

 1 import java.util.ArrayList;
 2 import java.util.Iterator;
 3 import java.util.List;
 4 
 5 public class BookList {
 6     private List<Book> bookList;
 7     private int index;
 8     private Iterator iterator;
 9     
10     public BookList() {
11         bookList = new ArrayList<Book>();
12     }
13     
14     //添加书籍
15     public void addBook(Book book) {
16         bookList.add(book);
17     }
18     
19     //删除书籍
20     public void deleteBook(Book book) {
21         int bookIndex = bookList.indexOf(book);
22         bookList.remove(bookIndex);
23     }
24     
25 //    //判断是否有下一本书
26 //    public boolean hasNext() {
27 //        if(index >= bookList.size()) {
28 //            return false;
29 //        }
30 //        return true;
31 //    }
32 //    
33 //    //获得下一本书
34 //    public Book getNext() {
35 //        return bookList.get(index++);
36 //    }
37     
38 //    public List<Book> getBookList() {
39 //        return bookList;
40 //    }
41     
42     public Iterator Iterator() {
43         return new Itr();
44     }
45     //迭代
46     private class Itr implements Iterator{
47 
48         public boolean hasNext() {
49             if(index >= bookList.size()) {
50                 return false;
51             }
52             return true;
53         }
54 
55         public Object next() {
56             return bookList.get(index++);
57         }
58 
59         public void remove() {
60             
61         }
62     }
63 }

测试

 1 import java.util.Iterator;
 2 
 3 public class MainClss {
 4     public static void main(String[] args) {
 5         BookList bookList = new BookList();
 6         
 7         Book book1 = new Book("010203","Java编程思想",90);
 8         Book book2 = new Book("010204","Java从入门到精通",60);
 9         
10         bookList.addBook(book1);
11         bookList.addBook(book2);
12         
13 //        while(bookList.hasNext()) {
14 //            Book book = bookList.getNext();
15 //            book.display();
16 //        }
17         
18 //        List<Book> bookDateList = bookList.getBookList();
19 //        for(int i = 0; i < bookDateList.size(); i++) {
20 //            Book book = bookDateList.get(i);
21 //            book.display();
22 //        }
23         
24         Iterator iter = bookList.Iterator();
25         while(iter.hasNext()) {
26             Book book = (Book) iter.next();
27             book.display();
28         }
29     }
30 }
原文地址:https://www.cnblogs.com/justdoitba/p/9034540.html