Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

@Pattern(name="Iterator", scope=Object, purpose=Behavioural, participants={"Iterator","ConcreteIterator","Aggregate","ConcreteAggregate"})

Package dk.rode.thesis.iterator

Implementations and examples of the Iterator design pattern [Gamma95, p.257].

See:
          Description

Interface Summary
ValueProcessor<E> A value processor is used by a processable sequence to process values during processing.
 

Class Summary
IterableSequence<E> An iterable sequence is a bounded sequence that can be accessed as any other iterable object.
LoggingValueProcessor A logging value processor logs the first X number of sequence values during processing, where X is supplied at construction time.
Main Iterator tests.
ProcessableSequence<E> A processable sequence represents an internal iterator that can iterate over bounded sequences using a value processor to perform the actual processing of delivered sequence values.
SequenceIterator<E> A sequence iterator is an external iterator that can iterate over bounded sequences.
 

Package dk.rode.thesis.iterator Description

Implementations and examples of the Iterator design pattern [Gamma95, p.257].

Intent:

This package provides both external and internal iterators that can be used to iterate over bounded Sequence values instead of using Sequence.next().

For external iterators, the Iterator participant is represented by Java's built-in java.util.Iterator interface. A single class representing the ConcreteIterator participant is supplied here, namely SequenceIterator.

The Aggregate participant is represented by Java's built in java.lang.Iterable interface and the ConcreteAggregate participant is represented by the IterableSequence class.

Regarding internal iterators, the Iterator participant is represented by the ProcessableSequence class. A processable sequence decorates any bounded sequence to allow processing of sequence values using a ValueProcessor instance supplied at iteration time. Hence, decoration and composition is used in favour of inheritance as used by Gamma et al. for their internal iterators [Gamma95, p.267-269]. Any ValueProcessor implementation is considered a representation of the ConcreteIterator participant for internal iterators, for example the LoggingValueProcessor class.

No classes have been defined to represent the Aggregate and ConcreteAggregate participants for internal iterators. Java defines no standard interface for internal iterators. Here, the construction of ProcessableSequence objects is left to the discretion of the context using them.

UML Class Diagram:

Implementation notes:
The IterableSequence and ProcessableSequence classes utilises the Decorator pattern to allow any bounded Sequence to become iterable. The delegation of work to a ValueProcessor from a ProcessableSequence instance is an application of the Strategy pattern.

Unless care is taken to copy a sequence before it is used by the iterators defined here, the iterators cannot be considered robust. They are not fail-fast as Java's iterators normally are. They cannot mimic a null iterator [Gamma95, p.262-263] because of the Sequence requirement to always deliver a value, i.e. sequences are never empty. As the sequences to iterate over are decorated by the iterators, the iterators only have access to public Sequence behaviour.

Author:
Gunni Rode / rode.dk

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.