【設計模式】—— 迭代模式Iterator

  前言:【模式總覽】——————————by xingoohtml

  模式意圖

  提供一個方法按順序遍歷一個集合內的元素,而又不須要暴露該對象的內部表示。ide

  應用場景

  1 訪問一個聚合的對象,而不須要暴露對象的內部表示函數

  2 支持對聚合對象的多種遍歷this

  3 對遍歷不一樣的對象,提供統一的接口。spa

  模式結構

  Iterator 定義訪問的接口code

/** * 抽象的迭代,有判斷結束和下一個,獲取當前元素等函數 * @author xingoo * */
interface Iterator{ void first(); void next(); boolean isDone(); Object currentItem(); }

 

  ConcreteIterator 具體的迭代器,跟蹤聚合內的元素htm

/** * 具體的迭代類 * @author xingoo * */
class ConcreteIterator implements Iterator{ private ConreteAggregate agg; private int index = 0; private int size = 0; public ConcreteIterator(ConreteAggregate agg) { this.agg = agg; size = agg.size(); index = 0; } public void first() { index = 0; } public void next() { if(index < size){ index++; } } public boolean isDone() { return (index >= size); } public Object currentItem() { return agg.getElement(index); } }

 

  Aggregate 提供聚合的接口對象

/** * 聚合的類 * @author xingoo * */
abstract class Aggregate{ public Iterator createIterator(){ return null; } }

 

  ConcreteAggregate 具體的聚合blog

/** * 具體的聚合對象,擁有大小,建立迭代子等函數 * @author xingoo * */
class ConreteAggregate extends Aggregate{ private Object[] obj = {"test1","test2","test3","test4"}; public Iterator createIterator(){ return new ConcreteIterator(this); } public Object getElement(int index){ if(index < obj.length){ return obj[index]; }else{ return null; } } public int size(){ return obj.length; } }

  所有代碼接口

 1 package com.xingoo.Iterator;  2 /**  3  * 聚合的類  4  * @author xingoo  5  *  6  */
 7 abstract class Aggregate{  8     public Iterator createIterator(){  9         return null; 10  } 11 } 12 /** 13  * 抽象的迭代,有判斷結束和下一個,獲取當前元素等函數 14  * @author xingoo 15  * 16  */
17 interface Iterator{ 18     void first(); 19     void next(); 20  boolean isDone(); 21  Object currentItem(); 22 } 23 /** 24  * 具體的聚合對象,擁有大小,建立迭代子等函數 25  * @author xingoo 26  * 27  */
28 class ConreteAggregate extends Aggregate{ 29     private Object[] obj = {"test1","test2","test3","test4"}; 30     public Iterator createIterator(){ 31         return new ConcreteIterator(this); 32  } 33     public Object getElement(int index){ 34         if(index < obj.length){ 35             return obj[index]; 36         }else{ 37             return null; 38  } 39  } 40     public int size(){ 41         return obj.length; 42  } 43 } 44 /** 45  * 具體的迭代類 46  * @author xingoo 47  * 48  */
49 class ConcreteIterator implements Iterator{ 50     private ConreteAggregate agg; 51     private int index = 0; 52     private int size = 0; 53     
54     public ConcreteIterator(ConreteAggregate agg) { 55         this.agg = agg; 56         size = agg.size(); 57         index = 0; 58  } 59 
60     public void first() { 61         index = 0; 62  } 63 
64     public void next() { 65         if(index < size){ 66             index++; 67  } 68  } 69 
70     public boolean isDone() { 71         return (index >= size); 72  } 73 
74     public Object currentItem() { 75         return agg.getElement(index); 76  } 77     
78 } 79 /** 80  * 客戶端 使用方法 81  * @author xingoo 82  * 83  */
84 public class Client { 85     private Iterator it; 86     private Aggregate agg = new ConreteAggregate(); 87     public void operation(){ 88         it = agg.createIterator(); 89         while(!it.isDone()){ 90             System.out.println(it.currentItem().toString()); 91  it.next(); 92  } 93  } 94     public static void main(String[] args) { 95         Client client = new Client(); 96  client.operation(); 97  } 98 }
View Code

  運行結果

test1 test2 test3 test4
相關文章
相關標籤/搜索