迭代子模式
http://www.cnblogs.com/java-my-life/archive/2012/05/22/2511506.html
- class MainClass {
- public void operation() {
- Object[] objArray = { "One", "Two", "Three", "Four", "Five", "Six" };
- // 建立聚合物件
- Aggregate agg = new ConcreteAggregate(objArray);
- // 迴圈輸出聚合物件中的值
- Iterator it = agg.createIterator();
- while (!it.isDone()) {
- System.out.println(it.currentItem());
- it.next();
- }
- }
- public static void main(String[] args) {
- MainClass client = new MainClass();
- client.operation();
- }
- }
- abstract class Aggregate {
- /**
- * 工廠方法,建立相應迭代子物件的介面
- */
- public abstract Iterator createIterator();
- }
- class ConcreteAggregate extends Aggregate {
- private Object[] objArray = null;
- /**
- * 構造方法,傳入聚合物件的具體內容
- */
- public ConcreteAggregate(Object[] objArray) {
- this.objArray = objArray;
- }
- @Override
- public Iterator createIterator() {
- return new ConcreteIterator(this);
- }
- /**
- * 取值方法:向外界提供聚集元素
- */
- public Object getElement(int index) {
- if (index < objArray.length) {
- return objArray[index];
- } else {
- return null;
- }
- }
- /**
- * 取值方法:向外界提供聚集的大小
- */
- public int size() {
- return objArray.length;
- }
- }
- interface Iterator {
- /**
- * 迭代方法:移動到第一個元素
- */
- public void first();
- /**
- * 迭代方法:移動到下一個元素
- */
- public void next();
- /**
- * 迭代方法:是否為最後一個元素
- */
- public boolean isDone();
- /**
- * 迭代方法:返還當前元素
- */
- public Object currentItem();
- }
- class ConcreteIterator implements Iterator {
- // 持有被迭代的具體的聚合物件
- private ConcreteAggregate agg;
- // 內部索引,記錄當前迭代到的索引位置
- private int index = 0;
- // 記錄當前聚集物件的大小
- private int size = 0;
- public ConcreteIterator(ConcreteAggregate agg) {
- this.agg = agg;
- this.size = agg.size();
- index = 0;
- }
- /**
- * 迭代方法:返還當前元素
- */
- @Override
- public Object currentItem() {
- return agg.getElement(index);
- }
- /**
- * 迭代方法:移動到第一個元素
- */
- @Override
- public void first() {
- index = 0;
- }
- /**
- * 迭代方法:是否為最後一個元素
- */
- @Override
- public boolean isDone() {
- return (index >= size);
- }
- /**
- * 迭代方法:移動到下一個元素
- */
- @Override
- public void next() {
- if (index < size) {
- index++;
- }
- }
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2135811/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 設計模式之迭代子模式設計模式
- 關於單子模式[求助]模式
- JAVA單子模式(static 應用)Java模式
- php正則逆向引用與子模式分析PHP模式
- 深入分析正規表示式的子模式模式
- 從迭代器模式到迭代協議模式協議
- Python迭代和迭代器詳解Python
- 迭代器
- Jacobi迭代、Gauss_Seidel迭代和最佳因子SOR迭代的比較IDE
- Python可迭代的物件與迭代器Python物件
- 迭代器,迭代器塊和資料管道
- 不用for迭代 --手工訪問迭代器中的元素.
- Python進階:迭代器與迭代器切片Python
- 可迭代物件 vs 迭代器 vs 生成器物件
- Python生成器、迭代器、可迭代物件Python物件
- TypeScript迭代器TypeScript
- 迭代器模式模式
- Iterator迭代器
- 版本迭代更新
- java迭代器Java
- ListIterator迭代器
- PHP 物件迭代PHP物件
- Python之可迭代物件、迭代器、生成器Python物件
- 你知道JavaScript中的可迭代物件與迭代器嗎JavaScript物件
- JavaScript的迭代函式與迭代函式的實現JavaScript函式
- 機器學習之迭代方法機器學習
- Python 迭代器Python
- 迭代器模式(Iterator)模式
- JavaScript 之迭代方法JavaScript
- javascript 之迭代器JavaScript
- Iterables和迭代器
- Python迭代器Python
- 迭代器總結
- Python:迭代器Python
- PHP 迭代器 (轉)PHP
- 周鴻禕:做產品體驗先把自己切換到二傻子模式模式
- 手寫自定義迭代器,秒懂迭代器底層原理
- 這坑貨 (迭代+迭代終止條件(由誤差控制))