Summary

The FLYWEIGHT pattern lets you share access to objects, such as characters, chemicals, and borders, that may appear in large quantities. The flyweight objects must be immutable, a feature you can establish by extracting the immutable part of the class that you want to share. To ensure that your flyweight objects are shared, you have to provide a factory in which clients can find flyweights, and you have to enforce the use of this factory. Visibility modifiers give you some control over how other developers access your code. Inner classes take this further, letting you guarantee that a class is accessible by only its containing class. By ensuring that clients use your flyweight factory properly, you can provide safe, shared access to what ...

Get Design Patterns Java™ Workbook 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.