Flyweight Design Pattern

The Flyweight Design Pattern is one of the Structural Design Patterns. It is used to reduce memory usage by sharing common data between multiple objects instead of storing duplicate copies.

In many applications, thousands or even millions of similar objects are created. Often, a large portion of their data is identical. Storing the same information repeatedly wastes memory and can negatively impact performance. The Flyweight pattern solves this problem by separating an object's state into:

  • Intrinsic State – Shared data that is identical for many objects. It is stored only once inside the Flyweight object.

  • Extrinsic State – Unique data that differs for each object. It is supplied by the client whenever the Flyweight is used.

By sharing intrinsic state, the Flyweight pattern dramatically reduces memory consumption while allowing each object to behave independently.

This document demonstrates two practical implementations of the Flyweight Design Pattern in C++.

Example 1: Forest Tree Simulation

Imagine building a game that renders an entire forest containing thousands of trees. Every Oak tree uses the same texture, color, and model, while only its position changes. Instead of storing the tree model thousands of times, we store it once and reuse it.

Program


Explanation

The TreeType class stores the intrinsic state, which includes the tree's name, texture, and color.

The Tree class stores the extrinsic state, which consists only of its position.

All three trees share the same TreeType object, avoiding duplicate storage of identical information.

Example 2: Text Editor Character Formatting

A text editor contains thousands of characters. Many characters share the same font, size, and color, differing only in the actual letter and its position.

Program


Explanation

The CharacterStyle object stores the shared formatting information such as font, size, and color.

Each Character stores only the unique data: the letter itself and its position.

Thousands of characters can reuse the same CharacterStyle object, significantly reducing memory consumption.

Characteristics of the Flyweight Design Pattern

PropertyDescription
Pattern TypeStructural Design Pattern
Core PrincipleShare common object state among many objects to reduce memory usage.
Main ComponentsFlyweight, Concrete Flyweight, Flyweight Factory, Context, and Client.
Intrinsic StateShared, immutable information stored inside the Flyweight object.
Extrinsic StateUnique information supplied by the client at runtime.
Primary BenefitGreatly reduces memory consumption when large numbers of similar objects exist.
Common ApplicationsGame engines, text editors, graphics rendering, map systems, particle systems, icons, fonts, and caching mechanisms.

Conclusion

The Flyweight Design Pattern optimizes memory usage by sharing common data across multiple objects instead of duplicating it. By separating intrinsic state from extrinsic state, applications can efficiently manage thousands or even millions of objects with minimal memory overhead. As demonstrated in the Forest Simulation and Text Editor examples, the Flyweight pattern is particularly valuable in systems where many objects share identical characteristics but differ only in a small amount of unique information.