00:00

Flyweight Design Pattern

Introduction

The Flyweight Design Pattern is a structural design pattern that helps reduce memory usage by sharing common objects instead of creating new ones repeatedly. It is mainly used when an application needs to create a large number of similar objects.

Simple Definition

The Flyweight pattern allows us to reuse existing objects by storing shared data in one place and avoiding duplicate object creation.

Real-Life Example

Think about a text editor. A document may contain thousands of characters, but only a few fonts. Instead of creating a new font object for every character, the editor reuses the same font object wherever possible. This saves memory and improves performance.

Key Concept

  • Intrinsic State: Data that can be shared (stored inside the object)
  • Extrinsic State: Data that changes and is provided from outside

When to Use Flyweight Pattern

  • When the application creates a large number of similar objects
  • When memory consumption is high
  • When objects can share common data

Flyweight Pattern Example in Java

Step 1: Create an Interface

This interface defines the operation that flyweight objects will perform.


public interface Shape {
    void draw(String color);
}

Step 2: Create a Concrete Flyweight Class

This class contains the shared (intrinsic) data.


public class Circle implements Shape {

    private final String shapeType = "Circle";

    @Override
    public void draw(String color) {
        System.out.println("Drawing a " + shapeType + " with color " + color);
    }
}

Step 3: Create a Flyweight Factory

The factory ensures that objects are reused instead of creating new ones every time.


import java.util.HashMap;
import java.util.Map;

public class ShapeFactory {

    private static final Map<String, Shape> circleMap = new HashMap<>();

    public static Shape getCircle() {
        Shape circle = circleMap.get("circle");

        if (circle == null) {
            circle = new Circle();
            circleMap.put("circle", circle);
            System.out.println("Creating a new Circle object");
        }
        return circle;
    }
}

Step 4: Client Code

The client uses the factory to get shared objects.


public class FlyweightDemo {

    public static void main(String[] args) {

        Shape circle1 = ShapeFactory.getCircle();
        circle1.draw("Red");

        Shape circle2 = ShapeFactory.getCircle();
        circle2.draw("Blue");

        Shape circle3 = ShapeFactory.getCircle();
        circle3.draw("Green");
    }
}

Output Explanation

Even though the draw method is called multiple times, the Circle object is created only once. Different colors are passed as extrinsic data.

Advantages of Flyweight Pattern

  • Reduces memory usage
  • Improves performance
  • Efficient handling of large numbers of objects

Disadvantages of Flyweight Pattern

  • Increases code complexity
  • Needs careful separation of shared and non-shared data

Conclusion

The Flyweight Design Pattern is useful when memory optimization is important. By sharing common objects and passing variable data from outside, applications can run faster and use fewer resources. It is commonly used in systems like text editors, graphics applications, and games.