1 public class WhiteIterator { 2 private Iterator it; 3 private Aggregate agg = new ConcreteAggregate(); 4 public void operation(){ 5 it = agg.createIterator(); 6 while(!it.isDone()){ 7 System.out.println(it.currentIterm()); 8 it.next(); 9 } 10 } 11 public static void main(String[] args) { 12 WhiteIterator wi = new WhiteIterator(); 13 wi.operation(); 14 } 15 16 } 17 18 //抽象彙集角色Aggregate 19 abstract class Aggregate{ 20 //工廠方法:返還一個迭代子對象 21 public Iterator createIterator(){ 22 return null; 23 } 24 } 25 //抽象迭代子角色 26 interface Iterator{ 27 //迭代方法:移動到第一個元素 28 void first(); 29 //迭代方法:移動到下一個元素 30 void next(); 31 //迭代方法:是不是最後一個元素 32 boolean isDone(); 33 //迭代方法:返回當前元素 34 Object currentIterm(); 35 } 36 //具體聚焦角色 37 class ConcreteAggregate extends Aggregate{ 38 private Object[] obj ={"one","two","three"}; 39 //工廠方法:返回一個迭代子對象 40 public Iterator createIterator(){ 41 return new ConcreteIterator(this); 42 } 43 //取值方法:向外界提供彙集元素 44 public Object getElement(int index){ 45 if(index<obj.length){ 46 return obj[index]; 47 }else{ 48 return null; 49 } 50 } 51 //取值方法向外界提供彙集的大小 52 public int size(){ 53 return obj.length; 54 } 55 } 56 57 //具體迭代子 58 class ConcreteIterator implements Iterator{ 59 private ConcreteAggregate agg; 60 private int index =0; 61 private int size =0; 62 63 //構造方法,接收一個具體彙集對象爲參量,使之能夠控制彙集對象 64 public ConcreteIterator(ConcreteAggregate agg){ 65 this.agg = agg; 66 size = agg.size(); 67 index =0; 68 } 69 // 70 71 @Override 72 public void first() { 73 index = 0; 74 } 75 76 @Override 77 public void next() { 78 if(index<size){ 79 index++; 80 } 81 } 82 83 @Override 84 public boolean isDone() { 85 return (index>=size); 86 } 87 88 @Override 89 public Object currentIterm() { 90 return agg.getElement(index); 91 } 92 93 }
1 public class BlackIterator { 2 private Iterator it; 3 private Aggregate agg = new ConcreteAggregate(); 4 public void operation(){ 5 it = agg.createIterator(); 6 while(!it.isDone()){ 7 System.out.println(it.currentIterm()); 8 it.next(); 9 } 10 } 11 public static void main(String[] args) { 12 BlackIterator bi = new BlackIterator(); 13 bi.operation(); 14 } 15 } 16 //抽象聚焦角色 17 abstract class Aggregate{ 18 //工廠方法:返回一個迭代子對象 19 public abstract Iterator createIterator(); 20 } 21 //抽象迭代子 22 interface Iterator{ 23 //迭代方法:移動到第一個元素 24 void first(); 25 //迭代方法:移動到下一個元素 26 void next(); 27 //迭代方法:是不是最後一個元素 28 boolean isDone(); 29 //迭代方法:返回當前元素 30 Object currentIterm(); 31 } 32 //具體彙集角色 33 class ConcreteAggregate extends Aggregate{ 34 private Object[] obj = {"one","two","three"}; 35 36 @Override 37 public Iterator createIterator() { 38 39 return new ConcreteIterator(); 40 } 41 //內部成員類:具體迭代子類 42 private class ConcreteIterator implements Iterator{ 43 private int currentIndex =0; 44 45 @Override 46 public void first() { 47 currentIndex = 0; 48 } 49 50 @Override 51 public void next() { 52 if(currentIndex <obj.length){ 53 currentIndex++; 54 } 55 } 56 57 @Override 58 public boolean isDone() { 59 return currentIndex==obj.length; 60 } 61 62 @Override 63 public Object currentIterm() { 64 return obj[currentIndex]; 65 } 66 67 } 68 }