Flyweight¶
Intent¶
Support large numbers of fine-grained objects efficiently by sharing common (intrinsic) state and externalizing varying (extrinsic) state.
Use When¶
- You have many similar objects and memory or allocation cost is a problem.
- Objects can be split into:
- intrinsic state: shareable, mostly immutable
- extrinsic state: supplied at call time (position, context, owner)
Prefer Something Else When¶
- The count isn’t high enough to justify complexity.
- Objects carry lots of mutable unique state (sharing won’t help).
Minimal Structure¶
Flyweightobject holding intrinsic stateFlyweightFactorycaching and reusing flyweights by a key- Client supplies extrinsic state when invoking operations
Implementation Steps¶
- Measure/confirm the memory or allocation pressure (don’t guess).
- Define a stable key for intrinsic state (string/tuple/enum).
- Make flyweights effectively immutable and safe to share.
- Move extrinsic data to the call site (method parameters) or a separate context object.
Pitfalls¶
- Accidental mutation: shared objects must not hold per-instance mutable state.
- Cache growth: define eviction or bound the key space if unbounded.
- Thread safety: shared caches and flyweights must be safe under concurrency.
Testing Checklist¶
- Factory returns the same instance for the same intrinsic key.
- Behavior is correct when extrinsic state changes across calls.
- If you add eviction/bounds, test cache policies explicitly.