|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectdk.rode.thesis.flyweight.CharacterFactory
@Participant(value="FlyweightFactory") public class CharacterFactory
A character factory creates and manages Character,
Word, and Sentence objects. The first two
types are flyweights, while the latter is an unshared
flyweight.
This factory supports an invariant that states that any
Letter can only contain a single char. Sub-classes
may override it to alter this behaviour.
Character factories are thread-safe.
Implementation notes:
The getWords() and getCharacters() method return
unmodifiable collections. This corresponds to protection proxies
as described in the Proxy pattern.
| Field Summary | |
|---|---|
private Map<String,Character> |
characters
The set of created characters so far. |
private Map<Word,Word> |
words
The set of created words so far. |
| Constructor Summary | |
|---|---|
CharacterFactory()
No-arg constructor. |
|
| Method Summary | ||
|---|---|---|
|
count(Class<C> type)
Returns the number of instances created by this factory of the specific Character type
supplied as type. |
|
int |
countWords()
Returns the number of created words by this factory. |
|
protected Character |
create(String character)
Factory method that creates a Character based on
the character string supplied as character. |
|
Character |
getCharacter(char character)
Returns the unique Character instance representing
the char value supplied as character. |
|
Character |
getCharacter(CharSequence character,
int index)
Returns the unique Character instance representing
the char value at index index in character. |
|
Character |
getCharacter(String character)
Returns the unique Character instance representing
the value supplied as character. |
|
Collection<Character> |
getCharacters()
Returns a read-only collection of the characters created by this factory. |
|
|
getCharacters(Class<C> type)
Returns the created characters of the type supplied as type, if ant. |
|
Sentence |
getSentence(CharSequence sentence,
int start,
int end)
Returns a new Sentence containing the words parsed from
string. |
|
Sentence |
getSentence(String sentence)
Returns a new Sentence containing the words parsed from
sentence. |
|
private Word |
getWord(List<Character> word)
Possibly creates and returns the Word corresponding to
the characters supplied as word. |
|
Word |
getWord(String word)
Returns a Word instance parsed from the letters in string. |
|
Collection<Word> |
getWords()
Returns a read-only collection of the words created by this factory. |
|
protected String |
normalise(String character)
Normalises the string representing a Character to
a format supported by this factory. |
|
String |
toString()
Return the string representation of this factory. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
private final Map<String,Character> characters
Never null, but may be empty.
private final Map<Word,Word> words
Never null, but may be empty.
| Constructor Detail |
|---|
public CharacterFactory()
| Method Detail |
|---|
public final <C extends Character> int count(Class<C> type)
Character type
supplied as type.
C - The type of character.type - The character type; cannot be null.
NullPointerException - If type is null.getCharacters(Class)public final int countWords()
protected Character create(String character)
Character based on
the character string supplied as character. Sub-classes can override this hook for additional functionality.
character - The normalised character string; never null.
Character instance; never null.normalise(String)public final Character getCharacter(char character)
Character instance representing
the char value supplied as character.
character - The char.
Character instance; never null.
public final Character getCharacter(CharSequence character,
int index)
Character instance representing
the char value at index index in character.
character - The char sequence supplying the desired char
at index; cannot be null.index - The index to use.
Character instance; never null.
NullPointerException - If character is null.
IndexOutOfBoundsException - If index is illegal.public final Character getCharacter(String character)
Character instance representing
the value supplied as character.
character - The char.
Character instance; never null.
NullPointerException - If character is null.
IllegalArgumentException - If character does not
contain a single char.public Collection<Character> getCharacters()
getWords()public final <C extends Character> List<C> getCharacters(Class<C> type)
type, if ant.
C - The type of character.type - The character type; cannot be null.
NullPointerException - If type is null.count(Class)
public final Sentence getSentence(CharSequence sentence,
int start,
int end)
Sentence containing the words parsed from
string.
sentence - The actual sentence as a string; cannot be null.start - The start index.end - The end index.
string; can be null if
string only contains white space characters.
NullPointerException - If sentence is null.
IndexOutOfBoundsException - If start or end
are illegal.public Sentence getSentence(String sentence)
Sentence containing the words parsed from
sentence.
sentence - The actual sentence as a string; cannot be null.
sentence; never null.
NullPointerException - If sentence is null.private final Word getWord(List<Character> word)
Word corresponding to
the characters supplied as word.
word - The characters forming the word, in order; never null.
Word instance; never null.public final Word getWord(String word)
Word instance parsed from the letters in string.
If more than one word is present in word, the first one
is returned.
word - The word as a string; cannot be null.
Word instance; never null.
NullPointerException - If word is null.public Collection<Word> getWords()
getCharacters()protected String normalise(String character)
Character to
a format supported by this factory. Sub-classes can override this hook for additional functionality.
character - The character string; cannot be null.
NullPointerException - If character is null.
IllegalArgumentException - If character is empty, or
if after trimming contains more than one character.create(String)public String toString()
toString in class Object
|
Gunni Rode / rode.dk | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||