Iterator Pattern 对象行为型模式
意图
提供一种方法顺序访问一个聚合对象中的各个元素,且不需要暴露该对象的内部表示
结构
其中:
- Iterator (迭代器) 定义访问和遍历元素的接口
- ConcreteIterator (具体迭代器) 实现迭代器接口;对该聚合遍历时跟踪当前位置
- Aggregate (聚合) 定义创建相应迭代器对象的接口
- ConcreteAggregate (具体聚合) 实现创建相应迭代器的接口,该操作返回 ConcreteIterator 的一个适当的实例
适用性
Iterator 模式适用于:
- 访问一个聚合对象的内容而无需暴露它的内部表示
- 支持对聚合对象的多种遍历
- 为遍历不同的聚合结构提供一个统一的接口
利用 Java 已实现迭代器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratorPattern {
public static void main(String[] args) {
List<Book> bookList = new ArrayList<>();
String[] books = {"Linear Algebra", "Algorithm"};
double[] prices = {25.8, 29.8};
for(int i = 0; i < 2; i++){
bookList.add(new Book(books[i], prices[i]));
}
// 访问元素 1
for(int i = 0; i < bookList.size(); i++){
Book book = bookList.get(i);
System.out.println("name: " + book.getName() + " price: " + book.getPrice());
}
// 访问元素 2
System.out.println("=====222=====");
for(Book book : bookList){
System.out.println("name: " + book.getName() + " price: " + book.getPrice());
}
// 访问元素 3 迭代器
System.out.println("=====333=====");
Iterator iterator = bookList.iterator();
while (iterator.hasNext()){
Book book = (Book) iterator.next();
System.out.println("name: " + book.getName() + " price: " + book.getPrice());
}
}
}
class Book{
private String name;
private double price;
public Book(String name, double price){
this.name = name;
this.price = price;
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
}
例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.List;
public class IteratorPattern {
public static void main(String[] args) {
BookAggregate bookAggregate = new BookAggregate();
String[] books = {"Linear Algebra", "Algorithm"};
double[] prices = {25.8, 29.8};
for(int i = 0; i < 2; i++){
bookAggregate.Add(new Book(books[i], prices[i]));
}
Iterator iterator = bookAggregate.CreateIterator();
while(iterator.hasNext()){
Book book = (Book)iterator.next();
System.out.println("name: " + book.getName() + " price: " + book.getPrice());
}
}
}
interface Iterator{
public boolean hasNext();
public Object next();
}
class BookIterator implements Iterator{
private int index;
private BookAggregate bookAggregate;
public BookIterator(BookAggregate bookAggregate){
this.index = 0;
this.bookAggregate = bookAggregate;
}
@Override
public boolean hasNext(){
if (index < bookAggregate.getSize()) return true;
return false;
}
@Override
public Object next(){
Object obj = bookAggregate.get(index);
index++;
return obj;
}
}
interface Aggregate{
public Iterator CreateIterator();
}
class BookAggregate implements Aggregate{
private List<Book> list = new ArrayList<>();
public void Add(Book book){
list.add(book);
}
public Book get(int index){
return list.get(index);
}
public int getSize(){
return list.size();
}
@Override
public Iterator CreateIterator(){
return new BookIterator(this);
}
}
class Book{
private String name;
private double price;
public Book(String name, double price){
this.name = name;
this.price = price;
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
}