MΓ³dulo 23 Β· Design Patterns (All 23 GoF) β LecciΓ³n 11 de 23 Β· ~6 min
Flyweight
Flyweight: share immutable objects to save memory
Problem: you'd otherwise create thousands of identical, unchanging objects β wasting memory.
PawWalk scenario: thousands of Dog records, but only a few dozen breeds. Intern each Breed once and share the single instance across every dog of that breed.
The realization is a cache: a hash keyed by the intrinsic value, returning the SAME object every time.