Design Pattern | Flyweight Pattern
What Is the Flyweight Pattern?
- The English word flyweight means “flyweight”, the lightest weight class in boxing.
- In the Flyweight pattern, weight refers to memory usage.
- The Flyweight pattern reduces memory usage by sharing instances as much as possible.
- In GoF design patterns, it is classified as a structural design pattern.
Flyweight Pattern Example Program
This program reads large-character text from files and displays it.
Class Diagram

1. BigChar Class
This class represents a large character.
BigChar.java
package com.devkuma.designpattern.structural.flyweight;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BigChar {
// String representing a large character, composed of '#', '.', and '\n'.
private String fontData;
public BigChar(char charName) {
try {
ClassLoader loader = BigChar.class.getClassLoader();
String file = loader.getResource("flyweight/big" + charName + ".txt").getFile();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
StringBuffer buf = new StringBuffer();
while ((line = reader.readLine()) != null) {
buf.append(line);
buf.append("\n");
}
reader.close();
this.fontData = buf.toString();
} catch (IOException e) {
this.fontData = charName + "?";
}
}
// Displays the large character.
public void print() {
System.out.print(fontData);
}
}
2. BigCharFactory Class
This class creates BigChar instances while sharing them.
BigCharFactory.java
package com.devkuma.designpattern.structural.flyweight;
import java.util.HashMap;
public class BigCharFactory {
// Manage already created BigChar instances.
private HashMap pool = new HashMap();
private static BigCharFactory singleton = new BigCharFactory();
private BigCharFactory() {
}
// Return the only instance.
public static BigCharFactory getInstance() {
return singleton;
}
// Create a BigChar instance(shared).
public synchronized BigChar getBigChar(char charName) {
BigChar bigChar = (BigChar) pool.get("" + charName);
if (bigChar == null) {
bigChar = new BigChar(charName);
pool.put("" + charName, bigChar);
}
return bigChar;
}
}
3. BigString Class
This class represents a “large string” made by collecting BigChar objects.
BigString.java
package com.devkuma.designpattern.structural.flyweight;
public class BigString {
private BigChar[] bigChars;
public BigString(String string) {
bigChars = new BigChar[string.length()];
BigCharFactory factory = BigCharFactory.getInstance();
for (int i = 0; i < bigChars.length; i++) {
bigChars[i] = factory.getBigChar(string.charAt(i));
}
}
public void print() {
for (int i = 0; i < bigChars.length; i++) {
bigChars[i].print();
}
}
}
4. Main Class
This is the class that executes the main processing.
Main.java
package com.devkuma.designpattern.structural.flyweight;
public class Main {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java Main digits");
System.out.println("Example: java Main 1234");
System.exit(0);
}
BigString bs = new BigString(args[0]);
bs.print();
}
}
5. Data
Refer to the txt files below.
/java-design-pattern-tutorial/src/resources/com/devkuma/designpattern/flyweight
6. Execution Result
The result below is produced by running the program with 0329 as the argument.
....######......
..##......##....
..##......##....
..##......##....
..##......##....
..##......##....
....######......
................
....######......
..##......##....
..........##....
......####......
..........##....
..##......##....
....######......
................
....######......
..##......##....
..........##....
......####......
....##..........
..##............
..##########....
................
....######......
..##......##....
..##......##....
....########....
..........##....
..##......##....
....######......
................
Advantages of the Flyweight Pattern
When instances are shared, there is no need to call new every time, so memory usage is lower. More generally, sharing instances can reduce the amount of resources needed to create instances. Resources are computer resources, and memory is one type of resource.
Time is also a type of resource. If creating an instance with new takes a certain amount of time each time, using the Flyweight pattern to share instances can reduce the number of times instances are created with new. This can improve program speed.