
本文共 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--; }}
������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
