Chapter 14. The Flyweight Pattern

In this chapter we take up the Flyweight pattern, which is used to avoid the overhead of large numbers of very similar classes.

Sometimes, 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 determine that the instances are fundamentally the same, except for a few parameters. If you can move those variables outside of the class instance and pass them in as part of a method call, you greatly reduce the number of separate instances by sharing them.

The Flyweight pattern provides an approach for handling such classes. It refers to the instance's intrinsic data that make the instance unique ...

Get Java™ Design Patterns: A Tutorial now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.