Master Software Design Patterns!
Subscribe to Ram N Java for clean code, architecture patterns, and practical Java tutorials!
SUBSCRIBE NOWWhy Use the Flyweight Design Pattern?
Imagine your application needs to render hundreds of thousands of graphical elements (like circles with various colors). If you instantiate a brand-new object using the new operator for each circle, your application will quickly consume massive amounts of heap memory and slow down.
The Flyweight Pattern is a structural design pattern that solves this problem by sharing common object instances instead of creating duplicate objects. By reusing existing instances, memory consumption drops drastically and overall application performance improves.
Understanding Intrinsic vs Extrinsic State
To successfully implement the Flyweight pattern, divide the object's data into two distinct states:
- Intrinsic State: Data that is constant, shared across instances, and stored inside the object memory (e.g., the underlying shape definition).
- Extrinsic State: Data that varies and depends on external context (e.g., color, coordinates, or runtime properties). This data is passed into methods or modified on the fly rather than storing separate objects.
Core Implementation in Java
The Flyweight factory maintains a lookup map (cache) of reusable objects. When a shape is requested, it checks the map first before creating a new instance.
import java.util.HashMap;
import java.util.Map;
// 1. Common Interface
interface Shape {
void draw();
}
// 2. Concrete Flyweight Class
class Circle implements Shape {
private String color;
private int x, y, radius;
public void setColor(String color) {
this.color = color;
}
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public void setRadius(int radius) { this.radius = radius; }
@Override
public void draw() {
System.out.println("Drawing Circle [Color: " + color +
", X: " + x + ", Y: " + y +
", Radius: " + radius +
", HashCode: " + System.identityHashCode(this) + "]");
}
}
// 3. Flyweight Factory
class ShapeFactory {
private static final Map<String, Shape> shapeMap = new HashMap<>();
public static Shape getShape(String shapeType) {
Shape shape = shapeMap.get(shapeType);
if (shape == null) {
if ("CIRCLE".equalsIgnoreCase(shapeType)) {
shape = new Circle();
shapeMap.put(shapeType, shape);
System.out.println("Created new Circle object in memory.");
}
}
return shape;
}
}
// 4. Demo / Client
public class FlyweightPatternDemo {
private static final String[] COLORS = { "Red", "Green", "Blue", "Orange", "Black" };
public static void main(String[] args) {
for (String color : COLORS) {
for (int i = 0; i < 2; i++) {
Circle circle = (Circle) ShapeFactory.getShape("CIRCLE");
circle.setColor(color);
circle.setX(i * 10);
circle.setY(i * 20);
circle.setRadius(15);
circle.draw();
}
}
}
}
Watch More Related Design Pattern Tutorials
Click here to watch in Youtube : https://www.youtube.com/watch?v=SKZQx1DdA9U
| Flyweight Design pattern - Implementation |
| Flyweight Design pattern - Class Diagram |
Shape.java