天天看點

設計模式(Design Pattern) - 行為型模式(Behavioral Pattern) - 疊代器模式(Iterator) - Java實作

設計模式(Design Pattern) - 行為型模式(Behavioral Pattern) - 疊代器模式(Iterator)

提供一種方法順序通路一個聚合對象中的各種元素,而又不暴露該對象的内部表示。

一、說明:

1、Iterator 疊代器(接口);

2、Collection 聚合對象(接口);

3、MyIterator 疊代器.實作類;

4、MyCollection 聚合對象.實作類。

二、Java實作,代碼如下:

1、Iterator

package com.java.designPattern.iterator;

/**
 * 疊代器 - 接口
 *
 */
public interface Iterator {

    /**
     * 前一個元素
     * @return
     */
    public Object previous();

    /**
     * 後一個元素
     * @return
     */
    public Object next();

    /**
     * 是否有下一個元素
     * @return
     */
    public boolean hasNext();

    /**
     * 擷取第一個元素
     * @return
     */
    public Object first();

}
           

2、Collection

package com.java.designPattern.iterator;

/**
 * 聚合對象 - 接口
 *
 */
public interface Collection {

    /**
     * 擷取疊代器
     * @return
     */
    public Iterator iterator();

    /**
     * 擷取聚合對象的元素
     * @param i
     * @return
     */
    public Object get(int i);

    /**
     * 擷取聚合對象大小
     * @return
     */
    public Integer size();

}
           

3、MyIterator

package com.java.designPattern.iterator;

/**
 * 疊代器.實作類
 *
 */
public class MyIterator implements Iterator {

    private Collection collection;
    private int pos = -;

    public MyIterator(Collection collection) {
        this.collection = collection;
    }

    @Override
    public Object previous() {
        if (pos > ) {
            pos--;
        }
        return collection.get(pos);
    }

    @Override
    public Object next() {
        if (pos < collection.size() - ) {
            pos++;
        }
        return collection.get(pos);
    }

    @Override
    public boolean hasNext() {
        if (pos < collection.size() - ) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Object first() {
        pos = ;
        return collection.get(pos);
    }

}
           

4、MyCollection

package com.java.designPattern.iterator;

/**
 * 聚合對象.實作類
 *
 */
public class MyCollection implements Collection {

    public Object[] obj;

    public MyCollection(Object[] obj) {
        this.obj = obj;
    }

    @Override
    public Iterator iterator() {
        return new MyIterator(this);
    }

    @Override
    public Object get(int i) {
        return this.obj[i];
    }

    @Override
    public Integer size() {
        return obj.length;
    }

}
           

5、Test

package com.java.designPattern.iterator;

/**
 * 測試類
 *
 */
public class Test {

    public static void main(String[] args) {
        Object[] obj = { "A", "B", "C", "D", "E" };
        MyCollection col = new MyCollection(obj);
        Iterator it = col.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println("--- --- ---");
        System.out.println(it.next());
        System.out.println(it.previous());
        System.out.println(it.first());
        System.out.println(it.previous());
    }

}
           

輸出:

A

B

C

D

E

E

D

A

A

繼續閱讀