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

Class ReflectiveVisitableSequence<E>

  extended by dk.rode.thesis.decorator.SequenceDecorator<E>
      extended by dk.rode.thesis.visitor.AbstractVisitableSequence<E>
          extended by dk.rode.thesis.visitor.ReflectiveVisitableSequence<E>
Type Parameters:
E - The type of values delivered by this visitable sequence.
All Implemented Interfaces:
Sequence<E>, Copyable<Sequence<E>>, StrictCopyable<Sequence<E>>, Stringable<Sequence<E>>, TypeVisitableSequence<E>, ValueVisitableSequence<E>

public class ReflectiveVisitableSequence<E>
extends AbstractVisitableSequence<E>

A reflective visitable sequence decorates a sequence to become value and type visitable.

Implementation notes:
The Decorator pattern is used to implement this visitable sequence.

The type visitable functionality is inherited from AbstractVisitableSequence, while the value visitable functionality is implemented via reflection in this class using type literals.

In case the decorated sequence is generic itself, i.e. the type of values to be delivered is specified using a type parameter, the value type cannot be inferred. In such cases, the SequenceVisitor.visitUnknown(Sequence, Object) value visitation method will be used. The visitUnknown method will also be used in case the type can be inferred, but is not supported by the SequenceValueVisitor interface, for example a sequence delivering java.math.BigDecimal values. Also note that only sequences delivering java.lang.String values can be inferred to the SequenceValueVisitor.visitStringValued(Sequence, Object) visitation method, though it accepts any sequence delivering java.lang.CharSequence values. Hence, decorating the HexDigitSequence, for example, will cause the visitUnknown method to be used because no visitCharSequenceValued visitation method is defined in the SequenceValueVisitor interface. An alternative to allow more flexible visitation method identification could be to use the Executor annotation instead.

While reflection is used to invoke the value visitation method, type safety is still guaranteed as the invocation is made privately, using parameter types and values known to be correct.

Gunni Rode / rode.dk
See Also:
DateValuedVisitableSequence, IntegerValuedVisitableSequence, StringValuedVisitableSequence, TypeLiteral

Nested Class Summary
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Field Summary
private  Method visitationMethod
          The value visitation method used by this sequence.
private static Method visitUnknown
          Default value visitation method, i.e. visit unknown type.
Constructor Summary
ReflectiveVisitableSequence(Sequence<E> sequence)
Method Summary
<P> void
accept(SequenceValueVisitor<P> visitor, P argument)
          Visits this visitable sequence using the value visitor supplied as visitor.
 ReflectiveVisitableSequence<E> copy()
          Returns a deep copy of this object.
Methods inherited from class dk.rode.thesis.visitor.AbstractVisitableSequence
Methods inherited from class dk.rode.thesis.decorator.SequenceDecorator
bounded, consistent, current, equals, getSequence, getStringablePolicy, hashCode, next, reset, state, toString, toString, unique
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface dk.rode.thesis.meta.model.Sequence
bounded, consistent, current, next, reset, state, unique
Methods inherited from interface dk.rode.thesis.strategy.Stringable
getStringablePolicy, toString

Field Detail


private final Method visitationMethod
The value visitation method used by this sequence.

Never null.


private static final Method visitUnknown
Default value visitation method, i.e. visit unknown type.

Never null.

Constructor Detail


public ReflectiveVisitableSequence(Sequence<E> sequence)

In case sequence is generic or delivers sequence values that cannot be visited by a value visitor, the SequenceVisitor.visitUnknown(Sequence, Object) value visitation method will be used.

sequence - The decorated sequence that will become visitable; cannot be null.
NullPointerException - If sequence is null.
Method Detail


public <P> void accept(SequenceValueVisitor<P> visitor,
                       P argument)
Description copied from interface: ValueVisitableSequence
Visits this visitable sequence using the value visitor supplied as visitor.

Type Parameters:
P - The type of visitor-supplied argument. Use Void for visitors that do not need an additional argument.
visitor - The visitor; never null.
argument - A context supplied argument, if any.
See Also:
SequenceValueScanner.scan(ValueVisitableSequence, SequenceValueVisitor, Object)


public ReflectiveVisitableSequence<E> copy()
Returns a deep copy of this object. The decorated sequence is not copied.

The copy; never null.

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.