Flyweight

Intent

  • Use sharing to support large numbers of fine-grained objects efficiently.

Problem

  • How can large numbers of fine-grained objects be supported efficiently?

Solution

  • Define separate Flyweight objects that store intrinsic (invariant) state.

  • Clients share Flyweight objects and pass in extrinsic (variant) state instead of creating an object each time it is needed.

Advantages

✅ Enables abstractions at the finest levels. ✅ Reduces memory usage.

Disadvantages

❌ Increases runtime-cost. ❌ Increases code complexity.

Last updated