Flyweight Pattern for Memory Efficiency
Discover the Flyweight pattern, which shares fine-grained objects to support large numbers of instances with minimal memory.
The Memory Problem
Some applications must hold huge numbers of similar objects: characters in a document, tiles in a game, particles in a simulation.
Naively creating one full object per element can exhaust memory. The Flyweight pattern attacks this directly.
Core Idea: Share What Repeats
Flyweight splits an object state into two parts:
- Intrinsic state: shared, context-independent (e.g. a glyph shape).
- Extrinsic state: unique per use, passed in from outside (e.g. position).
Only the intrinsic part is stored once and reused.
All lessons in this course
- Adapter and Decorator Patterns
- Facade and Proxy Patterns
- Composite and Bridge Patterns
- Flyweight Pattern for Memory Efficiency