Flyweight
Flyweight shares immutable intrinsic state across many logical objects while keeping per-use extrinsic state outside the shared object.
What you’ll understand
- Flyweight shares immutable intrinsic state across many logical objects while keeping per-use extrinsic state outside the shared object.
- A style factory caches immutable style data; glyph records keep only a style reference and their own position.
- Evaluate the tradeoff: The cache uses memory and lookup work. Sharing only helps when intrinsic state repeats, and mutable shared style state would make unrelated glyphs change together.
Useful first: Classes and Objects
The idea and when to recognize it
Flyweight shares immutable intrinsic state across many logical objects while keeping per-use extrinsic state outside the shared object.
- Requirement: A document contains many characters that reuse the same font style but have different positions.
- Change to anticipate: Reuse one style object per unique style while preserving every character position.
- Design goal: A style factory caches immutable style data; glyph records keep only a style reference and their own position.
Start with the concrete problem
A document contains many characters that reuse the same font style but have different positions.
Reuse one style object per unique style while preserving every character position.
| Direct implementation | Revised design |
|---|---|
| Allocating a full style for every character misses the intended sharing boundary. | A style factory caches immutable style data; glyph records keep only a style reference and their own position. |
| The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design. | The cache uses memory and lookup work. Sharing only helps when intrinsic state repeats, and mutable shared style state would make unrelated glyphs change together. |
Responsibilities and contracts
A style factory caches immutable style data; glyph records keep only a style reference and their own position.
- StyleFactory: Caches intrinsic styles. Public operations: getStyle(key)
- Style: Holds immutable shared data. Public operations: name
- Glyph: Owns its position. Public operations: position, style
Read the design
Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.
Preparing the design walkthrough…
Follow the interaction
Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.
Preparing the design walkthrough…
Implementation and expected output
Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.
Preparing the design walkthrough…
Try the idea in another setting
Map markers can share icon assets while retaining separate geographic coordinates.
- Name the new client contract before adapting the example.
- Identify which state belongs to each object and who controls its lifetime.
- Write one normal case and one boundary case before extending the design.
Tradeoffs and interview takeaways
The cache uses memory and lookup work. Sharing only helps when intrinsic state repeats, and mutable shared style state would make unrelated glyphs change together.
Common mistake: Allocating a full style for every character misses the intended sharing boundary.
- Explain the choice: A style factory caches immutable style data; glyph records keep only a style reference and their own position.
- Challenge your choice: The cache uses memory and lookup work. Sharing only helps when intrinsic state repeats, and mutable shared style state would make unrelated glyphs change together.
- Describe a requirement that would make the simpler implementation sufficient.
Design challenge and knowledge check
Preparing the design walkthrough…