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

@Pattern(name="State", scope=Object, purpose=Behavioural, participants={"Context","State","ConcreteState"})

Package dk.rode.thesis.state

Implementations and examples of the State design pattern [Gamma95, p.305].

See:
          Description

Interface Summary
FunctionalState<E> A functional state encapsulates specific state related functionality of a stateable sequence.
StateableSequence<E> A stateable sequence is implemented internally using functional state objects to determine the current functional state.
StepSequence A step sequence represents an unbounded integer value that is increased/decreased with a given step with each call to Sequence.next().
 

Class Summary
AbstractStateableSequence<E> An abstract stateable sequence implements the basic traits of a stateable sequence.
EvenSequence The even sequence represents the state generating even step sequence values.
Handler The handler class is used to perform the actual state change when the step sequence value goes from even to odd or vice versa.
Main State tests.
OddSequence The odd sequence represents the state generating odd step sequence values.
ReversiblePrimeSequence A reversible prime sequence returns with each call to next() the next prime number in the sequence of prime numbers smaller than maximum, where maximum is supplied at construction time, or a previously calculated prime number with each call to next() after ReversiblePrimeSequence.reverse() has been invoked.
StepSequence.Factory A factory to create StepSequence instances.
StepSequenceImpl An abstract class representing the basic traits of the StepSequence interface.
 

Enum Summary
ReversiblePrimeSequence.PrimeState A prime state represents a stateless functional state for all reversible prime sequences, while the actual internal state and attributes are stored in a given reversible prime sequence instance.
 

Package dk.rode.thesis.state Description

Implementations and examples of the State design pattern [Gamma95, p.305].

Intent:

Two different State implementations have been made: 1) Standard class/interface usage and 2) dynamic proxies.

For case number 1), the Context participant is represented by the StateableSequence interface, and more specifically by the implementing class ReversiblePrimeSequence. An abstract class implementing the basic functionality for any stateable sequence is also defined, AbstractStateableSequence, which ReversiblePrimeSequence therefore inherits.

The State participant is represented by the FunctionalState interface that describes how ConcreteState participants are encapsulated and accessed. ReversiblePrimeSequence defines a private enumeration representing different ConcreteState implementations corresponding to sequence behaviour such as initialise, next, reset, etc.

For case number 2), the Context participant is represented by the StepSequence type. StepSequence is a dynamic proxy that represents a sequence that will advance the sequence value a given step on each call to next(). Internally, it uses two states types, one representing even sequence values and one representing odd sequence values. The common sequence value type is the State participant, while the even and odd implementations represent the ConcreteState participant. Once the sequence value flips from even to odd or vice versa, the target object (even or odd instance) will change, not the sequence itself. This imitates dynamic inheritance as discussed by Gamma et al. [Gamma95, p.309].

UML Class Diagram:

Implementation notes:
State transitions are primarily initiated by the different ConcreteState implementations, but also by the ReversiblePrimeSequence upon construction and when reversed.

The Memento implementation illustrates how to guard access to public state methods in the GuardedSequenceMemento class.

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.