Iterator

Intent

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Class diagram of the original GoF pattern

structure-GoF:iterator

Improvement in MixJuice

Solved Problem(s)

Information hiding problem
Internal state of Aggregate should be accessible from Iterator but hidden from clients. (This problem is mentioned at p.262,line.18-23 in the GoF book.) Note that this problem is also solved by the package/nested-class mechanisms of Java.

Used programming technique(s)

Namespace separation of ConcreteAggregate and ConcreteIterator.

Consequences

Structure and pseudo code

structure:iterator_sample1

module aggregate {
  define class Aggregate {
    define abstract Iterator createIterator();
  }

  define class Iterator {
    define abstract void first();
    define abstract void next();
    define abstract boolean isDone();
    define abstract Object currentItem();
  }

  define class ConcreteAggregate extends Aggregate {
  }
  define class ConcreteIterator extends Iterator {
  }
}

module aggregate.implementation extends aggregate {
  class ConcreteAggregate {
    Iterator createIterator() {...}
  }
  class ConcreteIterator {
    // accessible to ConcreteAggregate directly.
    void first() {...}
    void next() {...}
    boolean isDone() {...}
    Object currentItem() {...}
  }
}

Akira TANAKA <akr@m17n.org>, Yuuji ICHISUGI <y-ichisugi@aist.go.jp>

Top