StructuralverifiedVerified

Flyweight

Minimizes memory usage by sharing fine-grained objects that represent repeated data, storing intrinsic state once and passing extrinsic state at call time.

Coming Soon

errorWhat Problem Does the Flyweight Pattern Solve?

Applications that create large numbers of similar objects—characters in a text editor, particles in a game, map tiles—consume excessive memory because each instance stores data that is identical across many objects. At scale this can exhaust available memory and degrade performance.

check_circleHow the Flyweight Pattern Works

Split object state into intrinsic (shared, immutable) and extrinsic (unique per use, passed as context). Store a single shared Flyweight instance per unique intrinsic state in a factory/cache. Clients pass the extrinsic state as method arguments at runtime. The dramatic reduction in live instances slashes memory consumption.

Flyweight Pattern Architecture

hourglass_empty

Rendering diagram...

Implementation by Language

lightbulb

Flyweight Pattern in the Real World

A book publisher doesn’t print a separate metal typeface block for every letter ‘e’ on every page. Instead, one block for ‘e’ (intrinsic state) is reused in every position, with the printer supplying the ink color and position (extrinsic state) each time it is stamped. Thousands of impressions share one piece of metal.

Frequently Asked Questions

helpHow does Flyweight differ from caching?

Caching stores complete objects to avoid re-computation. Flyweight splits objects into shared intrinsic state (immutable, stored once) and unique extrinsic state (passed in by the client at runtime). Flyweight is a structural pattern for memory optimization; caching is a performance strategy.

helpIs Flyweight still useful with modern hardware having gigabytes of RAM?

Yes, especially in scenarios involving millions of similar objects — game entities, text editors (one object per character), or map tiles. Even with abundant RAM, reducing object count improves cache locality and garbage collection performance, which affects responsiveness.