Skip to content

Understanding the Flyweight Design Pattern

Posted on:September 13, 2022 at 01:13 PM

The Flyweight design pattern is a powerful tool that helps optimize memory usage, especially when dealing with a large number of similar objects in an application. This pattern allows us to reduce memory consumption by sharing common, immutable parts of objects across multiple instances. Let’s delve into this pattern and understand how it can be applied to create more efficient software.

Table of contents

Open Table of contents

Sections

The Problem of Memory Usage

In software development, we often encounter scenarios where we need to handle a massive number of similar objects, each with its own unique data. Storing all this data directly in memory can quickly lead to an excessive memory footprint. For instance, if we have a game with numerous characters or sprites, storing each sprite’s data independently in memory can be inefficient and impractical.

The Visual Representation

To visualize this, let’s imagine a scenario where we want to display characters or images on the screen. A naive approach would be to store every single character’s or image’s data in memory, resulting in a substantial memory consumption. However, this isn’t ideal, especially when dealing with a plethora of small sprites in a game.

Instead, the Flyweight pattern suggests removing repetitive information and placing it in a centralized factory, reducing the memory needed. For example, if we have three elements to display, we would store only three instances in memory, even if we need to display them multiple times.

Applying the Flyweight Design Pattern

Let’s demonstrate how the Flyweight pattern can be implemented in a simple program. In our scenario, we’ll consider a game where we have fighters with different ranks: private, sergeant, and major.

Fighter Interface

First, we define the Fighter interface to represent a fighter’s behavior:

public interface Fighter {
    void draw();
}

Fighter Rank Enum

Next, we create an enum FighterRank to represent the different ranks:

public enum FighterRank {
    PRIVATE, SERGEANT, MAJOR
}

Fighter Factory

Now, let’s create a factory class FighterFactory to manage the creation of fighters.

public class FighterFactory {
    private Map<FighterRank, Fighter> fighters = new HashMap<>();

    public Fighter getFighter(FighterRank rank) {
        return fighters.computeIfAbsent(rank, key -> {
            System.out.println("Creating new " + key.toString() + " fighter.");
            return () -> System.out.println("Drawing " + key.toString() + " fighter.");
        });
    }
}

Fighter Implementation

Next, we create a class FighterImplementation that represents the actual fighters.

public class FighterImplementation implements Fighter {
    private FighterRank rank;

    public FighterImplementation(FighterRank rank) {
        this.rank = rank;
    }

    @Override
    public void draw() {
        System.out.println("Drawing " + rank.toString() + " fighter.");
    }
}

Army Class

Lastly, we have an Army class to organize and draw our fighters.

public class Army {
    private List<Fighter> fighters = new ArrayList<>();
    private FighterFactory factory = new FighterFactory();

    public void addFighter(FighterRank rank) {
        fighters.add(factory.getFighter(rank));
    }

    public void drawArmy() {
        for (Fighter fighter : fighters) {
            fighter.draw();
        }
    }
}

Conclusion

The Flyweight design pattern is a valuable tool for optimizing memory usage, making it particularly useful when dealing with a large number of similar objects. By centralizing common data and sharing it among multiple instances, we can significantly reduce memory footprint and improve application efficiency. Understanding and effectively implementing the Flyweight pattern can lead to more efficient, scalable, and optimized software systems.

Happy coding!