Exercise 3: 500 Trees Across 7 Species, and What Per-Tree Color Would Break — Possible Solution ==================================================================== VERIFYING THE 7-SPECIES COUNT ------------------------------ Planting 500 trees, each randomly assigned one of 7 distinct species (Oak, Pine, Birch, Maple, Willow, Cedar, Ash - each with its own fixed color and texture), using this chapter's own TreeType/Tree/ Forest classes unchanged: trees planted: 500 distinct TreeType objects: 7 Exactly one TreeType flyweight per species was created, regardless of how many of the 500 trees ended up being that species - matching this chapter's own 10,000-trees-to-3-species result, just at a different scale (500 trees, 7 species instead of 10,000 trees, 3 species). WHAT WOULD HAPPEN WITH A UNIQUE COLOR PER TREE ------------------------------ TreeType's cache key is (name, color, texture) - ALL THREE fields together, not just the species name. If every individual tree were given its own slightly different shade of color (even trees of the same species), each tree would produce a DIFFERENT cache key, since no two trees would share an identical (name, color, texture) triple anymore. The result: TreeType._cache would grow to hold up to 500 distinct entries - one per tree - instead of 7. The Flyweight pattern's whole benefit (many trees sharing a small number of underlying objects) would disappear almost entirely, because the thing being varied per-tree (color) is exactly the piece of data the Flyweight was designed to treat as SHARED, INTRINSIC state. WHY THIS MATTERS AS A DESIGN LESSON ------------------------------ This reveals the real design decision Flyweight forces: whatever data gets included in the shared key must be genuinely repeated across many instances, or the pattern provides no benefit at all. A per-tree color belongs on the Tree object itself as EXTRINSIC state (like x, y already are) - varying per instance, passed in rather than baked into the shared, cached object. The fix would be to move color out of TreeType's cache key entirely and pass it into draw() the same way x and y already are, keeping only the genuinely-species-wide texture and default shading as intrinsic state. WHY THIS WORKS AS AN ANSWER ------------------------------ The 7-species count is verified directly rather than assumed to match this chapter's own 3-species result, and the per-tree-color question is answered by tracing exactly how it interacts with TreeType's own cache key mechanism, correctly identifying that it's the choice of what counts as intrinsic vs. extrinsic state - not the pattern itself - that determines whether Flyweight actually saves anything.