且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java设计模式之Iterator模式

更新时间:2022-03-07 18:21:50

 

 

分类: 【java】
所谓Iterator模式,即是Iterator为不同的容器提供一个统一的访问方式。本文以java中的容器为例,模拟Iterator的原理。
参考:马士兵 Java设计模式 Iterator
1.首先定义一个容器Collection接口.
[java] view plaincopy
 
  1. package com.njupt.zhb.learn.iterator;  
  2.   
  3. public interface Collection {  
  4.     void add(Object o);  
  5.     int size();  
  6.     Iterator iterator();  
  7. }  

2.定义一个Iterator迭代器的接口
[java] view plaincopy
 
  1. package com.njupt.zhb.learn.iterator;  
  2.   
  3. public interface Iterator {  
  4.     Object next();  
  5.     boolean hasNext();  
  6. }  

3.定义一个ArrayList,实现Collection接口,并写一个实现了Iterator接口的内部类。
[java] view plaincopy
 
  1. package com.njupt.zhb.learn.iterator;  
  2.   
  3. import com.njupt.zhb.learn.iterator.Collection;  
  4.   
  5. public class ArrayList implements Collection {  
  6.     Object[] objects = new Object[10];  
  7.     int index = 0;  
  8.     public void add(Object o) {  
  9.         if(index == objects.length) {  
  10.             Object[] newObjects = new Object[objects.length * 2];  
  11.             System.arraycopy(objects, 0, newObjects, 0, objects.length);  
  12.             objects = newObjects;  
  13.         }  
  14.         objects[index] = o;  
  15.         index ++;  
  16.     }  
  17.       
  18.     public int size() {  
  19.         return index;  
  20.     }  
  21.       
  22.     public Iterator iterator() {  
  23.           
  24.         return new ArrayListIterator();  
  25.     }  
  26.       
  27.     private class ArrayListIterator implements Iterator {  
  28.         private int currentIndex = 0;  
  29.   
  30.         @Override  
  31.         public boolean hasNext() {  
  32.             if(currentIndex >= index) return false;  
  33.             else return true;  
  34.         }  
  35.   
  36.         @Override  
  37.         public Object next() {  
  38.             Object o = objects[currentIndex];  
  39.             currentIndex ++;  
  40.             return o;  
  41.         }  
  42.           
  43.     }  
  44. }  


4.编写测试程序TestMain
[java] view plaincopy
 
  1. package com.njupt.zhb.learn.iterator;  
  2. import com.njupt.zhb.learn.iterator.ArrayList;  
  3. public class TestMain {  
  4.     public static void main(String[] args) {  
  5.         Collection c = new ArrayList();  
  6.         for(int i=0; i<15; i++) {  
  7.             c.add("string "+i);  
  8.         }  
  9.         System.out.println(c.size());  
  10.         Iterator it = c.iterator();  
  11.         while(it.hasNext()) {  
  12.             Object o = it.next();  
  13.             System.out.println(o.toString() + " ");  
  14.         }  
  15.     }  
  16. }  

运行结果:

 

[html] view plaincopy
 
  1. 15  
  2. string 0   
  3. string 1   
  4. string 2   
  5. string 3   
  6. string 4   
  7. string 5   
  8. string 6   
  9. string 7   
  10. string 8   
  11. string 9   
  12. string 10   
  13. string 11   
  14. string 12   
  15. string 13   
  16. string 14   


 

从以上可以看出,设计模式到处用到面向对象中的多态接口调用子类中的函数。源代码下载:
http://download.csdn.net/detail/nuptboyzhb/5755295