Breaking down the Flyweight pattern
As part of the structural family of design patterns, the Flyweight pattern is all about optimization for performance, memory management, and reusability when dealing with vast numbers of objects. Instead of copying or pooling entire objects, the Flyweight pattern focuses on identifying (and separating) an object’s data into two categories: context-dependent and context-independent. Context-independent information can be shared between object instances, while context-dependent information can be injected and used without being stored. We call context-independent data intrinsic data and context-dependent extrinsic data (internal and external).
The Flyweight pattern is useful when:
- Your application uses a large number of objects that eat up considerable storage space.
- You have objects that store lots of extrinsic state, which could be paired down to a few shared objects once that extrinsic state is removed.
- You don...