java迭代子模式詳解
迭代子(Iterator)模式的結(jié)構(gòu):
迭代子模式可以順序訪問一個聚集中的元素而不必暴露聚集的內(nèi)部表象。
迭代子可分為外稟迭代子和內(nèi)稟迭代子。
外稟迭代子:適合于白箱聚集(白箱聚集就是向外界提供訪問自己內(nèi)部元素接口的聚集),由于迭代的邏輯是由聚集對象本身提供的,所以這樣的外稟迭代子角色往往僅僅保持迭代的游標(biāo)位置。所以具體迭代子角色是一個外部類,它的構(gòu)造函數(shù)接受一個具體聚集對象,從而可以調(diào)用這個聚集對象的迭代邏輯。
內(nèi)稟迭代子:適用于黑箱聚集(黑箱聚集不向外部提供遍歷自己元素對象的接口),由于黑箱聚集的元素對象只可以被聚集內(nèi)部成員訪問,所以內(nèi)稟迭代子只能是聚集內(nèi)部的成員子類。
簡單示范:
package test.edu.inter;
public interface IteratorObj {
/**
* 移動到第一個元素
*/
public void first();
/**
* 移動到下一個元素
*/
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;
/**
* 傳入聚合對象
*/
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());
}
}
}
運行結(jié)果:
12312 dasda dasd 12d asd
內(nèi)容擴充:在java聚集中的應(yīng)用
在java.util.Collection接口提供iterator()工廠方法返回一個Iterator類型對象,Collection接口的子類型AbstractList類的內(nèi)部成員類Itr實現(xiàn)Iterator接口。所以Itr是內(nèi)稟迭代子類,但是AbstractList也提供了自己的遍歷方法,所以它不是黑箱聚集,而是白箱聚集。其代碼如下:
import java.util.Iterator;
public interface Itr extends Iterator{
//再次調(diào)用next()方法時所用的指標(biāo)
int cursor = 0;
//最近一次調(diào)用時所用的指標(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()會檢查聚集的內(nèi)容是否剛剛被外界直接修改過(不是通過迭代子提供的remove()方法修改的)。如果在迭代子開始后,聚集的內(nèi)容被外界繞過迭代子對象而直接修改過年話,這個方法立即拋出異常。
另外:AbstractList類也提供了listIterator()方法,返回一個實現(xiàn)了Listiterator接口的類ListItr實例。ListIterator接口實現(xiàn)了正向迭代和逆向迭代,同時還提供了在迭代過程當(dāng)中安全修改列的內(nèi)容的方法。
Enumeration與Iterator的區(qū)別:(1)Enumeration沒有remove方法(2)Enumeration是在Vector中的element()方法中作用一個無名類實現(xiàn)的,它不支付Fail Fast,也就是說在迭代過程中,聚集對象被外界意外直接修改,則這個迭代過程還會立即捕獲任何異常。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
request.getParameter()方法的簡單理解與運用方式
在JavaWeb開發(fā)中,request對象扮演著至關(guān)重要的角色,它是HTTP請求的封裝,request.getParameter()用于獲取客戶端通過GET或POST方式發(fā)送的參數(shù),與之相對,request.setAttribute()用于在服務(wù)器端設(shè)置屬性,這些屬性只在一次請求中有效2024-10-10
Spring Cloud應(yīng)用實現(xiàn)配置自動刷新過程詳解
這篇文章主要介紹了Spring Cloud應(yīng)用實現(xiàn)配置自動刷新過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12
Java Idea TranslationPlugin翻譯插件使用解析
這篇文章主要介紹了Java Idea TranslationPlugin翻譯插件使用解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
mybatis-plus的自動填充時間的問題(添加到數(shù)據(jù)庫的時間比當(dāng)前時間多4個小時)
這篇文章主要介紹了mybatis-plus的自動填充時間的問題(添加到數(shù)據(jù)庫的時間比當(dāng)前時間多4個小時),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09

