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

dk.rode.thesis.singleton
Class MutatedSimpsonsFamilySequence

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<E>
      extended by dk.rode.thesis.meta.model.ArraySequence<String>
          extended by dk.rode.thesis.singleton.SimpsonsFamilySequence
              extended by dk.rode.thesis.singleton.MutatedSimpsonsFamilySequence
All Implemented Interfaces:
Sequence<String>, Copyable<Sequence<String>>, StrictCopyable<Sequence<String>>, Stringable<Sequence<String>>, Cloneable

public final class MutatedSimpsonsFamilySequence
extends SimpsonsFamilySequence
implements Cloneable

A mutated Simpsons family sequence is a sequence representing a random mutated sub-set of the primary family members in the one and only The Simpsons family.

This class can be sub-classed.

Implementation notes:
Even though the super class SimpsonsFamilySequence is a singleton class, this class is not. Creation and copying works like it does for any other non-singleton sequence. This sequence is also cloneable, but will still not create additional SimpsonsFamilySequence instances.

It would in fact be possible to create SimpsonsFamilySequence instances from the MutatedSimpsonsFamilySequence(int) and MutatedSimpsonsFamilySequence(MutatedSimpsonsFamilySequence) constructors without the call to a super-constructor, i.e. using new. A caller cannot determine how the super-class constructor is invoked from a sub-class, only from where. Hence, any singleton type that allows sub-classing has to trust the sub-classes to some extent. However, it is not possible even for sub-classes to invoke the super constructor from other places than constructors. The evilCreation() method is test method that will try and create a SimpsonsFamilySequence, but will fail because it is not a constructor.

Even though this class references static fields in SimpsonsFamilySequence class, it will not cause the SimpsonsFamilySequence singleton instance to be created.

Author:
Gunni Rode / rode.dk

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Field Summary
 
Fields inherited from class dk.rode.thesis.singleton.SimpsonsFamilySequence
members, SIZE
 
Fields inherited from class dk.rode.thesis.meta.model.ArraySequence
elements, index
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
MutatedSimpsonsFamilySequence()
          No-arg constructor.
MutatedSimpsonsFamilySequence(int members)
          Constructor.
MutatedSimpsonsFamilySequence(MutatedSimpsonsFamilySequence sequence)
          Copy constructor.
 
Method Summary
 MutatedSimpsonsFamilySequence clone()
          Returns a clone of this sequence.
 MutatedSimpsonsFamilySequence copy()
          Returns this instance.
 SimpsonsFamilySequence evilCreation()
          Tries to create a SimpsonsFamilySequence instance based on an off-limit constructor.
private static String[] getMutatedMembers(int members)
          Returns a mutated sub-set of the primary family members in The Simpsons family of size members.
 
Methods inherited from class dk.rode.thesis.singleton.SimpsonsFamilySequence
equals, getFamily, hashCode
 
Methods inherited from class dk.rode.thesis.meta.model.ArraySequence
bounded, consistent, current, next, reset, size, unique
 
Methods inherited from class dk.rode.thesis.meta.model.AbstractSequence
getStringablePolicy, state, toString, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface dk.rode.thesis.meta.model.Sequence
state
 
Methods inherited from interface dk.rode.thesis.strategy.Stringable
getStringablePolicy, toString
 

Constructor Detail

MutatedSimpsonsFamilySequence

public MutatedSimpsonsFamilySequence()
No-arg constructor.

Half the members of The Simpsons family will be included in this mutated sequence.


MutatedSimpsonsFamilySequence

public MutatedSimpsonsFamilySequence(int members)
Constructor.

The number of members of the The Simpsons family included in this sequence is specified by members.

Parameters:
members - The members; must be within [1, SimpsonsFamilySequence.SIZE].
Throws:
IllegalArgumentException - If members is illegal.

MutatedSimpsonsFamilySequence

public MutatedSimpsonsFamilySequence(MutatedSimpsonsFamilySequence sequence)
Copy constructor.

Parameters:
sequence - The sequence to copy; cannot be null.
Throws:
NullPointerException - If sequence is null.
Method Detail

clone

public MutatedSimpsonsFamilySequence clone()
                                    throws CloneNotSupportedException
Returns a clone of this sequence.

Overrides:
clone in class Object
Returns:
A clone of this sequence; never null.
Throws:
CloneNotSupportedException - If the cloning fails.

copy

public MutatedSimpsonsFamilySequence copy()
Description copied from class: SimpsonsFamilySequence
Returns this instance.

Specified by:
copy in interface Sequence<String>
Specified by:
copy in interface Copyable<Sequence<String>>
Overrides:
copy in class SimpsonsFamilySequence
Returns:
This instance; never null.

evilCreation

public SimpsonsFamilySequence evilCreation()
                                    throws SingletonError
Tries to create a SimpsonsFamilySequence instance based on an off-limit constructor. Doing so should cause a SingletonError to be thrown! If not, it constitutes an internal error and the singleton invariant is broken.

Returns:
Should never return.
Throws:
SingletonError - Always thrown.

getMutatedMembers

private static final String[] getMutatedMembers(int members)
Returns a mutated sub-set of the primary family members in The Simpsons family of size members.

Parameters:
members - The members; must be within [1, SimpsonsFamilySequence.SIZE].
Returns:
The sub-set; never null, and never empty.
Throws:
IllegalArgumentException - If members is illegal.

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.