设计模式-行为型04-迭代子模式(Iterator)
发布日期:2021-05-10 03:41:15 浏览次数:12 分类:精选文章

本文共 4061 字,大约阅读时间需要 13 分钟。

������������������Iterator Pattern���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Iterator���������������������Collection������������������������������������������������ abstraction���

���������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������������������

  • ���������������Collection������������������������������������������������������������������������������������������������������������������������������������

  • ������������������Iterator Interface������������������������������������������������������������������������

    • hasNext()������������������������������������
    • next()���������������������������
    • remove()������������������������
    • etc.������������������������������������������
  • ���������������������Iterator Implementation���������������������������������������������������������������������������������������������������������������������������

  • ������������Client���������������������������������������������������������������������������������������������������������������������������������������������������������

  • ���������������������������

    ���������

  • ������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������
  • ������������ ���������������������������������������������������������������������������
  • ���������

  • ���������������������������������������������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������������������������
  • ������������������������������

    ������������������������������������������������������������������������������������

    • ������������������������Java������������������List���Set���������������������������������������������������������������������������������
    • ���������������������������������������������������������������������������������������������������������������
    • ���������������������SQL���������������������������������cursor���������������������������������������������������������������
    • ������������������������������������������������������������������������������������������������������������

    ������������������

    // ���������������public interface Iterator {    boolean hasNext();    Object next();    void remove();}// ������������public interface Collection {    Iterator iterator();    Object get(int index);    int size();}// ������������������public class MyCollection implements Collection {    private Object[] elements = new Object[10];    private int size = 0;    public Iterator iterator() {        return new MyIterator(this);    }    public Object get(int index) {        return elements[index];    }    public int size() {        return size;    }}// ���������������public class MyIterator implements Iterator {    private MyCollection collection;    public MyIterator(MyCollection c) {        this.collection = c;    }    public boolean hasNext() {        return collection.size() > 0;    }    public Object next() {        return collection.get(0); // ���������������������������    }    public void remove() {        collection.elements[0] = null;        collection.size--;    }}

    ������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:设计模式-行为型05-责任链模式(Chain of Responsibility)
    下一篇:设计模式-行为型03-观察者模式(Observer)

    发表评论

    最新留言

    关注你微信了!
    [***.104.42.241]2025年04月24日 17时37分06秒