Build a Shared Glyph Cache

Flyweight pattern

Challenge 6 of Create & Share ObjectsIntermediate~55 minFree

Build a text renderer with the Flyweight pattern: share one immutable Glyph object per character across every position it appears, and pass position (extrinsic state) in per render call. You will grow a GlyphFactory cache, prove object identity is reused, keep extrinsic state out of the shared object, lay out a whole document with shared glyphs, and finally key the cache by (char, style) so styled variants stay separate.

5 steps

  1. 1Cache one glyph per character
  2. 2Count the unique glyphs
  3. 3Pass position as extrinsic state
  4. 4Lay out a document with shared glyphs
  5. 5Key the cache by char and style
Start step 1