迭代子(Iterator)模式的結(jié)構(gòu):
迭代子模式可以順序訪問一個(gè)聚集中的元素而不必暴露聚集的內(nèi)部表象。
迭代子可分為外稟迭代子和內(nèi)稟迭代子。
外稟迭代子:適合于白箱聚集(白箱聚集就是向外界提供訪問自己內(nèi)部元素接口的聚集),由于迭代的邏輯是由聚集對(duì)象本身提供的,所以這樣的外稟迭代子角色往往僅僅保持迭代的游標(biāo)位置。所以具體迭代子角色是一個(gè)外部類,它的構(gòu)造函數(shù)接受一個(gè)具體聚集對(duì)象,從而可以調(diào)用這個(gè)聚集對(duì)象的迭代邏輯。
內(nèi)稟迭代子:適用于黑箱聚集(黑箱聚集不向外部提供遍歷自己元素對(duì)象的接口),由于黑箱聚集的元素對(duì)象只可以被聚集內(nèi)部成員訪問,所以內(nèi)稟迭代子只能是聚集內(nèi)部的成員子類。
簡單示范:
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
package test.edu.inter; public interface IteratorObj { /** * 移動(dòng)到第一個(gè)元素 */ public void first(); /** * 移動(dòng)到下一個(gè)元素 */ public boolean hasNextItem(); /** * 返還當(dāng)前元素 */ public Object currentItem(); } package test.edu.inter; public interface DataSet { public IteratorObj getIterator(); } package test.edu.inter; public class Iterator1 implements IteratorObj { private Dataobj set; private int size; private int index= 0 ; public Iterator1(Dataobj set){ this .set = set; this .size = set.getSize(); } @Override public void first() { // TODO Auto-generated method stub this .index = 0 ; } @Override public boolean hasNextItem() { if (index<size){ return true ; } return false ; } @Override public Object currentItem() { Object ob = set.getItem(index); if (index<size){ index++; } return ob; } } package test.edu.inter; public class Dataobj implements DataSet { private Object[] objArray = null ; /** * 傳入聚合對(duì)象 */ public Dataobj(Object[] objArray){ this .objArray = objArray; } @Override public IteratorObj getIterator() { return new Iterator1( this ); } public Object getItem( int index){ return objArray[index]; } public int getSize(){ return objArray.length; } } package test.edu.inter; public class Client { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String[] str={ "12312" , "dasda" , "dasd" , "12d" , "asd" }; Dataobj ao = new Dataobj(str); IteratorObj io = ao.getIterator(); while (io.hasNextItem()){ System.out.println(io.currentItem()); } } } |
運(yùn)行結(jié)果:
1
2
3
4
5
|
12312 dasda dasd 12d asd |
內(nèi)容擴(kuò)充:在java聚集中的應(yīng)用
在java.util.Collection接口提供iterator()工廠方法返回一個(gè)Iterator類型對(duì)象,Collection接口的子類型AbstractList類的內(nèi)部成員類Itr實(shí)現(xiàn)Iterator接口。所以Itr是內(nèi)稟迭代子類,但是AbstractList也提供了自己的遍歷方法,所以它不是黑箱聚集,而是白箱聚集。其代碼如下:
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
|
import java.util.Iterator; public interface Itr extends Iterator{ //再次調(diào)用next()方法時(shí)所用的指標(biāo) int cursor = 0 ; //最近一次調(diào)用時(shí)所用的指標(biāo) int lastRet = - 1 ; int expectedModCount = modCount; public boolean hasNext(){ return cursor!=size(); } public Object next(){ try { Object next = get(cursor); checkForComodification(); lastRet = cursor++; return next; } catch (IndexOutOfBoundsException e){ checkForComodification(); throw new NoSuchElementException(); } } //刪除最后遍歷過的元素,remove()方法只能刪除最后遍歷的元素 public void remove(){ if (lastRet ==- 1 ) throw new IllegalStateException(); checkForComodification(); try { AbstractList. this .remove(lastRet); if (lastRet<cursor) cursor--; lastRet = - 1 ; expectedModCount = modCount; } catch (IndexOutOfBoundsException e){ throw new ConcurrentModificationException(); } } public void checkForComodification(){ if (modCount!=expectedModCount) throw new ConcurrentModificationException(); } } |
其中的modCount、get(cursor)等變量和方法均是AbstractList類所擁有,Itr可以直接使用。方法checkForComodification()會(huì)檢查聚集的內(nèi)容是否剛剛被外界直接修改過(不是通過迭代子提供的remove()方法修改的)。如果在迭代子開始后,聚集的內(nèi)容被外界繞過迭代子對(duì)象而直接修改過年話,這個(gè)方法立即拋出異常。
另外:AbstractList類也提供了listIterator()方法,返回一個(gè)實(shí)現(xiàn)了Listiterator接口的類ListItr實(shí)例。ListIterator接口實(shí)現(xiàn)了正向迭代和逆向迭代,同時(shí)還提供了在迭代過程當(dāng)中安全修改列的內(nèi)容的方法。
Enumeration與Iterator的區(qū)別:(1)Enumeration沒有remove方法(2)Enumeration是在Vector中的element()方法中作用一個(gè)無名類實(shí)現(xiàn)的,它不支付Fail Fast,也就是說在迭代過程中,聚集對(duì)象被外界意外直接修改,則這個(gè)迭代過程還會(huì)立即捕獲任何異常。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。