📢 本文由 gemini-3-flash-preview 翻譯
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;
}
}
|