Summary
Phew, that was an involved chapter (because why would performance optimization be easy, right?). But before you start looking for applicable spots to shoehorn the Flyweight pattern into your projects, let’s review some key topics. First, you’ll get the most out of the Flyweight pattern when you’re creating lots and lots of objects with both context-independent and -dependent data, and when those two data categories can be easily partitioned. The pattern will also come in handy when the context-dependent data can be computed rather than stored.
The Flyweight pattern has five components: the Flyweight interface, concrete Flyweights, unshared Flyweights, a Flyweight factory, and, of course, the client using it all. A clean flyweight interface is one that has operations that act on the object’s extrinsic state, acting as an injection point for any context-dependent information. Concrete flyweight objects implement the flyweight interface and store...