Flyweight Design Pattern Explained

Flyweight minimizes memory usage by sharing as much data as possible between many similar objects, rather than storing duplicate data in each one.

Published November 14, 2026

The Flyweight pattern saves memory by sharing the common, unchanging part of many objects instead of duplicating it. Only the unique part is stored per object.

The concept

Intrinsic state (shared, immutable) lives in a flyweight; extrinsic state (position, id) is supplied by the caller.

A factory returns existing flyweights from a cache. It helps when you have thousands of similar objects, like characters in a document or trees in a game.

Share the heavy part

10,000 trees, each with its own 5 MB texture = 50 GB
10,000 trees sharing 3 textures + (x, y) each = ~15 MB

Practical examples

Flyweight factory

const types = new Map()
function treeType(name, texture) {
  const key = name + texture
  if (!types.has(key)) types.set(key, { name, texture })
  return types.get(key)
}
const forest = Array.from({ length: 10000 }, (_, i) => ({ x: i, y: i * 2, type: treeType('oak', 'oak.png') }))

Strings are interned

String a = "hi"; String b = "hi";   // same pooled object

Check sharing

forest[0].type === forest[1].type // true

Common mistakes

  • Mutable shared state corrupting all users.
  • Using it when memory is not a problem.
  • Forgetting extrinsic data must be passed in.

Key takeaways

  • Share immutable common data.
  • Factory caches flyweights.
  • Use for huge numbers of similar objects.

FAQ

Is it the same as caching?

Related: it is caching for object parts specifically to cut memory.

Real examples?

String interning, glyph rendering, particle systems.

More General articles