
C SC 335, Spring 2007 Flyweight Design Pattern
Please read this before your next section on 17-Oct or 18-Oct, 2007
Constructing many objects is a relatively slow operation for a computer to perform. It takes CPU time
and memory to store objects; therefore, when possible, in large programs we should try to reduce the
number of objects we create while the program is running. But how can we do this?
One way to reduce object construction is to flyweight objects, if possible. Flyweighting means
reusing objects and variables, usually ones that are not changeable. If instances of a class that contain the
same information can be used interchangeably, the Flyweight pattern allows a program to avoid the
expense of multiple instances that contain the same information by sharing one instance.
The Flyweight pattern was used by the designers of Java for better performance. For example,
equivalent Strings in Java are flyweighted; if a program creates two String references that refer to the
String “hello”, both reference variables will refer to the same string object. This saves time and memory
by avoiding creating a second identical “hello” object. It is legal and safe to do this, because Strings are
immutable in Java—they cannot be directly modified. So there is no danger by having both variables
point to the same String, since they cannot modify it.
How can we implement a flyweighting system? It bears similarity to other patterns in Java, like
the Singleton pattern we discussed in class, and the Factory pattern, which we haven’t discussed. If we
have a class whose instances are unchangeable and often equivalent, we can make our class’s
constructor private, then create a static method to get a “new” instance of the class. If this “new”
instance is one that we have never created before, then we actually construct a new object. However, if
the “new” instance is equivalent/identical to another previously constructed instance, we can just return
the old instance.
Example from Biology Software Project
Biologists who analyze proteins using computational methods typically have characters and strings to
represent them. Proteins are made up of linear structures of 20 different molecules, called amino acids.
For convenience and brevity, amino acids are usually represented by characters. For example, the amino
acid tryptophan is represented by a 'W' and the amino acid methionine is represented by 'M'. A peptide, a
chain of amino acids, can be represented as a string of these characters, "NPVGLRQGID", for example.
An inspired but not very astute young computational biologist decided to write a class representing a
single amino symbol. Her class, AminoAcid, can be used to make immutable (can't be modified)
AminoAcid objects that can be strung together using lists and other linear data structures to represent a
peptide. The problem is that since peptides can be very long, these lists of objects take up an unnecessary
amount of memory. Typical lengths used for certain types of analysis can be around 500 to 1000 amino
acids long. Rewrite the AminoAcid class so programs that use it can be more memory efficient by
guaranteeing that no two objects with the same exact state can be instantiated and therefore stored in
memory.
public class AminoAcid {
private String myName;
private char myToken;
public AminoAcid(String name, char token) {
myName = name;
myToken = token;
}
public String getName() {
return myName;
}
public char getToken() {
return myToken;
}