行为型模式--迭代器模式

来源:互联网 发布:linux cgi 编辑:程序博客网 时间:2024/05/20 19:14

1.迭代器角色定义了遍历的接口,但是没有规定由谁来控制迭代。在Java collection的应用中,是由客户程序来控制遍历的进程,被称为外部迭代器;还有一种实现方式便是由迭代器自身来控制迭代,被称为内部迭代器。外部迭代器要比内部迭代器灵活、强大,而且内部迭代器在java语言环境中,可用性很弱。

  2.在迭代器模式中没有规定谁来实现遍历算法。好像理所当然的要在迭代器角色中实现。因为既便于一个容器上使用不同的遍历算法,也便于将一种遍历算法应用于不同的容器。但是这样就破坏掉了容器的封装??容器角色就要公开自己的私有属性,在java中便意味着向其他类公开了自己的私有属性。

  那我们把它放到容器角色里来实现好了。这样迭代器角色就被架空为仅仅存放一个遍历当前位置的功能。但是遍历算法便和特定的容器紧紧绑在一起了。

  而在Java Collection的应用中,提供的具体迭代器角色是定义在容器角色中的内部类。这样便保护了容器的封装。但是同时容器也提供了遍历算法接口,你可以扩展自己的迭代器。

  好了,我们来看下Java Collection中的迭代器是怎么实现的吧。

  //迭代器角色,仅仅定义了遍历接口

  

 

     public interface Iterator {

  boolean hasNext();

  Object next();

  void remove();

  }

  //容器角色,这里以List为例。它也仅仅是一个接口,就不罗列出来了

  //具体容器角色,便是实现了List接口的ArrayList等类。为了突出重点这里指罗列和迭代器相关的内容

  //具体迭代器角色,它是以内部类的形式出来的。AbstractList是为了将各个具体容器角色的公共部分提取出来而存在的。

  public abstract class AbstractList extends AbstractCollection implements List {

  ……

  //这个便是负责创建具体迭代器角色的工厂方法

  public Iterator iterator() {

  return new Itr();

  }

  //作为内部类的具体迭代器角色

  private class Itr implements Iterator {

  int cursor = 0;

  int lastRet = -1;

  int expectedModCount = modCount;

  public boolean hasNext() {

  return cursor != size();

  }

  public Object next() {

  checkForComodification();

  try {

  Object next = get(cursor);

  lastRet = cursor++;

  return next;

  } catch(IndexOutOfBoundsException e) {

  checkForComodification();

  throw new NoSuchElementException();

  }

  }

  public void remove() {

  if (lastRet == -1)

  throw new IllegalStateException();

  checkForComodification();

  try {

  AbstractList.this.remove(lastRet);

  if (lastRet < cursor)

  cursor--;

  lastRet = -1;

  expectedModCount = modCount;

  } catch(IndexOutOfBoundsException e) {

  throw new ConcurrentModificationException();

  }

  }

  final void checkForComodification() {

  if (modCount != expectedModCount)

  throw new ConcurrentModificationException();

  }

  }

     转自:http://blog.sina.com.cn/s/blog_4080505a0101e6kj.html

0 0