Skip to content
StackDecode
System Design
15. Software Design PatternsAnimated walkthrough

Flyweight Pattern

Share common data to save memory.

The idea

Many objects share the same intrinsic state, such as glyph shapes in a text editor.

Watch it work

The steps light up one at a time. Use the controls to pause or step through.

  1. 1Separate shared and unique state
  2. 2Store shared state once
  3. 3Pass unique state in
  4. 4Reuse flyweights
1/4

Step by step

  1. 1Separate shared and unique state
  2. 2Store shared state once
  3. 3Pass unique state in
  4. 4Reuse flyweights

Where it is used

Large numbers of similar objects.

Watch out for

More complex code for memory gains.

Related