Instead of creating a separate object for every instance, the Flyweight Pattern stores shared, immutable data in reusable flyweight objects while keeping unique data outside the flyweight.
This significantly reduces the number of objects created and is especially useful when an application must manage a very large number of similar objects.
The Flyweight Pattern is commonly used in text editors, game development, graphics rendering, caching, and object pooling.
Structure
The Flyweight Pattern consists of the following participants.1. The Flyweight defines the shared interface.
2. The Concrete Flyweight stores the intrinsic (shared) state.
3. The Flyweight Factory creates and reuses flyweight objects.
4. The Client supplies the extrinsic (unique) state whenever it uses a flyweight.

Java Implementation
Suppose a text editor displays thousands of characters. Rather than creating a separate font object for every character, all characters using the same font share a single font object.Create the flyweight interface.
public interface Font {
void render(String character);
}
Create the concrete flyweight.
public class ArialFont implements Font {
@Override
public void render(String character) {
System.out.println(character + " rendered using Arial.");
}
}
Create the flyweight factory.
import java.util.HashMap;
import java.util.Map;
public class FontFactory {
private static final Map<String, Font> fonts = new HashMap<>();
public static Font getFont(String name) {
return fonts.computeIfAbsent(name, key -> new ArialFont());
}
}
The client reuses shared flyweight objects.
public class Main {
public static void main(String[] args) {
Font font1 = FontFactory.getFont("Arial");
Font font2 = FontFactory.getFont("Arial");
font1.render("A");
font2.render("B");
System.out.println(font1 == font2);
}
}
Output:
A rendered using Arial.
B rendered using Arial.
true
How It Works?
The client requests a font from the FontFactory instead of creating one directly.The factory checks whether a flyweight for the requested font already exists. If it does, the existing object is returned; otherwise, a new flyweight is created, stored, and reused for future requests.
The shared font information represents the intrinsic state, while the character being rendered represents the extrinsic state supplied by the client during each operation.
By sharing common objects, the application avoids creating thousands of duplicate font instances, reducing memory consumption while maintaining the same functionality.
Advantages
1. It significantly reduces memory usage by sharing common objects.2. It improves performance when working with large numbers of similar objects.
3. Shared objects are reused instead of repeatedly created.
4. It centralizes object creation through a factory.
Disadvantages
1. The pattern increases design complexity by separating intrinsic and extrinsic state.2. Clients must manage the extrinsic state explicitly.
3. It provides little benefit when the number of objects is small.
JDK Examples
The Flyweight Pattern appears throughout the Java ecosystem.1. The Integer.valueOf(), Long.valueOf(), and other wrapper class valueOf() methods reuse cached immutable objects instead of creating new instances for frequently used values.
2. The String.intern() method stores identical string values in the string pool so that multiple references can share the same immutable String object.
Summary
The Flyweight Pattern reduces memory consumption by sharing immutable objects that contain common state while allowing clients to supply unique state separately.By reusing existing objects instead of repeatedly creating identical ones, the pattern improves memory efficiency and scalability in applications that manage large numbers of similar objects.