|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | ENUM CONSTANTS | FIELD | METHOD | DETAIL: ENUM CONSTANTS | FIELD | METHOD |
java.lang.Object java.lang.Enum<DanishAlphabetSequence> dk.rode.thesis.singleton.DanishAlphabetSequence
@Participant(value="Singleton") @Singleton(value="valueOf", arguments="Instance") public enum DanishAlphabetSequence
A singleton alphabet sequence for letters in the Danish alphabet:
a
though z
as well as æ
, ø
, and
å
, all lower-cased.
This class cannot be sub-classed, but is serializable.
Implementation notes:
By using an enumeration with a single constant
to represent
the singleton instance of this sequence, many singleton traits are enforced by
Java: the constructor has to be private and cannot be instantiated from elsewhere,
not even via reflection. The sequence type is guaranteed not cloneable, but
is serializable! Lazy loading is also possible. There is no need to implement
hashCode()
and equals(Object)
, as no other sequence will be
identical. Identity (==) comparison can be used. The singleton instance can
still be referenced through a static singleton method, but also through the
constant. We consider a Singleton implementation using enumerations in this manner
for an application of the
Singleton-as-Single-Constant
idiom.
Passing arguments to a singleton type is difficult, though. The type cannot
inherit functionality as the java.lang.Enum
class
is already inherited. Hence, composition has to be used in combination with
interface implementation.
The inherited static method valueOf(String)
from java.lang.Enum
can also be used to initialise and/or fetch the singleton instance. This is
utilised here to be able to fetch this sequence from a
StatelessSingletonRegistry
instance as it is annotated
with the Singleton
enumeration.
NorwegianAlphabetSequence
,
SimpsonsFamilySequence
,
SimpsonsAndBouvierFamilySequence
,
SmileySequence
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence |
---|
Sequence.State |
Enum Constant Summary | |
---|---|
Instance
The DanishAlphabetSequence singleton instance
to use. |
Field Summary | |
---|---|
private Sequence<String> |
sequence
Delegate sequence storing the actual letters in the Danish alphabet. |
static int |
SIZE
The size of the Danish alphabet. |
Method Summary | |
---|---|
boolean |
bounded()
Returns true if this sequence is bounded, i.e. |
boolean |
consistent()
Returns true if this sequence is consistent, i.e. deliver the same values, in order, after restart or reset . |
DanishAlphabetSequence |
copy()
Returns this instance. |
String |
current()
Returns the current element from this sequence. |
StringablePolicy<? super Sequence<String>> |
getStringablePolicy(StringablePolicy<? super Sequence<String>> policy)
Always return a non-null policy: policy is not null: policy is returned. |
String |
next()
Returns the next element from this sequence. |
void |
reset()
Resets this sequence to start over if it is consistent. |
Sequence.State |
state()
Returns the internal state of this sequence. |
String |
toString()
|
CharSequence |
toString(StringablePolicy<? super Sequence<String>> policy)
Returns a char sequence representation of this stringable object using the format determined by policy or the
default policy
in case policy is null. |
boolean |
unique()
Returns true if this sequence deliver a given sequence value at most one time until reset or restarted. |
static DanishAlphabetSequence |
valueOf(String name)
Returns the enum constant of this type with the specified name. |
static DanishAlphabetSequence[] |
values()
Returns an array containing the constants of this enum type, in the order they are declared. |
Methods inherited from class java.lang.Enum |
---|
clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, valueOf |
Methods inherited from class java.lang.Object |
---|
getClass, notify, notifyAll, wait, wait, wait |
Enum Constant Detail |
---|
public static final DanishAlphabetSequence Instance
DanishAlphabetSequence
singleton instance
to use. Is not initialised until first time accessed.
Field Detail |
---|
private final Sequence<String> sequence
Implementation notes:
Delegation has to be used as enumerations cannot extend other
classes as they already extend the java.lang.Enum
class.
This cannot be declared static because then it might be loaded after
Instance
, as Instance
is in fact a separate class. As
only a single constant is ever created, this works just fine.
public static final int SIZE
Implementation notes:
Accessing this static field will before Instance
will not cause the singleton instance to be created!
NorwegianAlphabetSequence.SIZE
,
SimpsonsFamilySequence.SIZE
,
SimpsonsAndBouvierFamilySequence.SIZE
,
SmileySequence.SIZE
,
Constant Field ValuesMethod Detail |
---|
public boolean bounded()
Sequence
The same type of sequence may represent both bounded and
unbounded sequences and the behaviour is determined and fixed
at construction time.
Bounded sequences will restart if they deliver
consistent
values.
bounded
in interface Sequence<String>
Sequence.unique()
public boolean consistent()
Sequence
reset
.
Only bounded
consistent sequences will restart.
Consistent sequences need not deliver unique
sequence values.
Instances of the same type of sequences are always consistent or inconsistent; it is determined at design time, not construction time.
consistent
in interface Sequence<String>
public DanishAlphabetSequence copy()
copy
in interface Sequence<String>
copy
in interface Copyable<Sequence<String>>
public String current()
Sequence
This method can be invoked even if Sequence.next()
has not been invoked yet, thus delivering the initial
value of this sequence.
current
in interface Sequence<String>
public StringablePolicy<? super Sequence<String>> getStringablePolicy(StringablePolicy<? super Sequence<String>> policy)
Stringable
policy
is not null: policy
is returned.
policy
is null: a default, non-null policy is returned.
getStringablePolicy
in interface Stringable<Sequence<String>>
policy
- The supplied policy; can be null.
Stringable.toString(StringablePolicy)
public String next()
Sequence
next
in interface Sequence<String>
Sequence.current()
,
Sequence.state()
public void reset()
Sequence
If this sequence is consistent
, the
sequence will restart.
reset
in interface Sequence<String>
public Sequence.State state()
Sequence
state
in interface Sequence<String>
public String toString()
toString
in class Enum<DanishAlphabetSequence>
public CharSequence toString(StringablePolicy<? super Sequence<String>> policy)
Stringable
policy
or the
default
policy
in case policy
is null.
In Foo
, a typical implementation of this method
could be:
public CharSequence toString(StringablePolicy<? super Foo> policy) { return this.There are two approaches to formatting this stringable object into a char sequence representation:getStringablePolicy
(policy).toString
(this); }
policy
decide the entire format,
as in the Foo
example above; or
policy
to format part of the overall
representation, for example letting this method append
certain text regardless of the policy used.
StringablePolicy.toString(Object)
method, for
example in case multiple stringable objects should be formatted
into an overall representation.
In case an implementation uses the approach from bullet 2), care
must be take to respect the policy hints
so the overall format remains meaningful.
toString
in interface Stringable<Sequence<String>>
policy
- The policy to dictate the formatting; can be null, in
which case the result of toString
method
is returned.
StringablePolicy.Type
public boolean unique()
Sequence
reset
or restarted.
Unbounded
sequences that are unique will never
deliver the same sequence value more than once.
The same type of sequence may represent both unique and not unique sequences and the behaviour is determined and fixed at construction time.
unique
in interface Sequence<String>
Sequence.consistent()
public static DanishAlphabetSequence valueOf(String name)
name
- the name of the enum constant to be returned.
IllegalArgumentException
- if this enum type has no constant
with the specified name
NullPointerException
- if the argument is nullpublic static DanishAlphabetSequence[] values()
for (DanishAlphabetSequence c : DanishAlphabetSequence.values()) System.out.println(c);
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | ENUM CONSTANTS | FIELD | METHOD | DETAIL: ENUM CONSTANTS | FIELD | METHOD |