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

@Pattern(name="Flyweight", scope=Object, purpose=Structural, participants={"Flyweight","ConcreteFlyweight","UnsharedConcreteFlyweight","FlyweightFactory","Client"})

Package dk.rode.thesis.flyweight

Implementations and examples of the Flyweight design pattern [Gamma95, p.195].


Interface Summary
Character A character represents a letter, symbol, or whitespace in a Word.
Textual<T> A textual object represents a flyweight object that is a char sequence and comparable to its own type.

Class Summary
AbstractCharacter An abstract character implements the basic traits of the Character interface.
CharacterFactory A character factory creates and manages Character, Word, and Sentence objects.
Letter A letter represents a single letter that is not a whitespace or contain symbolic characters.
Main Flyweight tests.
Sentence A sentence represents an ordered sequence of words making up that sentence.
Symbol A symbol represents a single symbol that is not a whitespace or a letter.
Whitespace A whitespace represents a single white space character.
Word A word represents an ordered sequence of characters making up that word.

Enum Summary
CharacterPolicy Default policies for formatting Character objects into char sequences (not part of the core Flyweight implementation).

Package dk.rode.thesis.flyweight Description

Implementations and examples of the Flyweight design pattern [Gamma95, p.195].


Here, the Flyweight participant is represented by the Textual interface. This package supplies three flyweight types, namely the Character interface and the Word and Sentence classes.

A standard abstract implementation of the Character interface is supplied in form of the AbstractCharacter class, and each implementation corresponds to the ConcreteFlyweight participant, for example Letter and Symbol.

The Word class also represent the ConcreteFlyweight participant, while the Sentence class represents the UnsharedConcreteFlyweight participant.

Flyweights are constructed using the FlyweightFacctory participant, which is represented by the CharacterFactory class.

The Client participant is the test application, i.e. the Main class.

UML Class Diagram:

Implementation notes:
Both Word and Sentence are in fact implementations of the ArraySequence class: the elements of a sentence is the words it contains, while the elements of a word is the letters it contains. Hence, Sentence, Word, and Character objects form a Composite-like structure. This fits very well with the general semantics of Sequence, for example that any Word must have at least one Character, or a Sequence at least one Word.

All textual flyweights in this package are also Stringable.

The internal states used by the ReversiblePrimeSequence class can be seen as a degenerate application of the Flyweight pattern.

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.