当前位置: 移动技术网 > IT编程>开发语言>Java > java迭代子模式详解

java迭代子模式详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

迭代子(iterator)模式的结构:
迭代子模式可以顺序访问一个聚集中的元素而不必暴露聚集的内部表象。
迭代子可分为外禀迭代子内禀迭代子
外禀迭代子:适合于白箱聚集(白箱聚集就是向外界提供访问自己内部元素接口的聚集),由于迭代的逻辑是由聚集对象本身提供的,所以这样的外禀迭代子角色往往仅仅保持迭代的游标位置。所以具体迭代子角色是一个外部类,它的构造函数接受一个具体聚集对象,从而可以调用这个聚集对象的迭代逻辑。
内禀迭代子:适用于黑箱聚集(黑箱聚集不向外部提供遍历自己元素对象的接口),由于黑箱聚集的元素对象只可以被聚集内部成员访问,所以内禀迭代子只能是聚集内部的成员子类。
简单示范:

package test.edu.inter; 
 
public interface iteratorobj { 
  
  /** 
  * 移动到第一个元素 
  */ 
 public void first(); 
 /** 
  * 移动到下一个元素 
  */ 
 public boolean hasnextitem(); 
 /** 
  * 返还当前元素 
  */ 
 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()); 
  } 
 } 
} 

运行结果:

12312 
dasda 
dasd 
12d 
asd 

内容扩充:在java聚集中的应用
在java.util.collection接口提供iterator()工厂方法返回一个iterator类型对象,collection接口的子类型abstractlist类的内部成员类itr实现iterator接口。所以itr是内禀迭代子类,但是abstractlist也提供了自己的遍历方法,所以它不是黑箱聚集,而是白箱聚集。其代码如下:

import java.util.iterator;
public interface itr extends iterator{
   //再次调用next()方法时所用的指标
   int cursor = 0;
   //最近一次调用时所用的指标
   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()会检查聚集的内容是否刚刚被外界直接修改过(不是通过迭代子提供的remove()方法修改的)。如果在迭代子开始后,聚集的内容被外界绕过迭代子对象而直接修改过年话,这个方法立即抛出异常。
另外:abstractlist类也提供了listiterator()方法,返回一个实现了listiterator接口的类listitr实例。listiterator接口实现了正向迭代和逆向迭代,同时还提供了在迭代过程当中安全修改列的内容的方法。
enumeration与iterator的区别:(1)enumeration没有remove方法(2)enumeration是在vector中的element()方法中作用一个无名类实现的,它不支付fail fast,也就是说在迭代过程中,聚集对象被外界意外直接修改,则这个迭代过程还会立即捕获任何异常。

以上就是本文的全部内容,希望对大家的学习有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网