17
The Flyweight Pattern
Sometimes in programming, it seems that you need to generate a very large number of small class instances to represent data. You can greatly reduce the number of different classes that you need to instantiate if you can recognize that the instances are fundamentally the same except for a few parameters. If you can move those variables outside the class instance and pass them in as part of a method call, the number of separate instances can be greatly reduced by sharing them.
The Flyweight design pattern provides an approach for handling such classes. It refers to the instance’s intrinsic data, which makes the instance unique, and the extrinsic data, which is passed in as arguments. The Flyweight pattern is appropriate ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access