Revision Notes/Design Patterns

Field Entry  -  Design Patterns

 Coreintermediate

Flyweight Pattern

Shares common, immutable state across many fine-grained objects to dramatically reduce memory usage when dealing with huge numbers of similar objects.

#structural#flyweight#memory#sharing#pool#gof

Field Note - Real-world Analogy

Think of it like this

Like characters in a word processor. The character "A" appears thousands of times in a document, but the font, size, and glyph data for "A" is only stored once — that's the flyweight (intrinsic state). Each occurrence of "A" just stores its position on the page — that's the extrinsic state. The glyph is shared; the position is unique.

Field Observations - Key Topics Covered

01

The problem: you're building a forest renderer that places 1 million tree objects on screen. Each tree stores its type name, color, texture, X position, and Y position. If all those million trees share the same type/color/texture data (because it's a pine forest), you're storing those large textures in memory 1 million times — an obvious waste.

4 more observations inside

Sign in to read every key point in full

Unlock Full Note

Ready to go deeper?

Open the complete revision note.

Sign in to read every field observation in full, including a runnable code example and side-by-side comparisons with related patterns.

Read Full Notes