Recognizing Flyweight

To provide for the sharing of flyweight objects, you need a class with a method other than a constructor that returns the object to be shared. This lets you control flyweight creation, ensuring that only one instance of any particular flyweight exists. There is an example of a flyweight factory in a Swing class that is a factory for Swing components. This class comment says, “wherever possible, this factory will hand out references to shared ... instances.” We used this class in Chapter 4, Facade, and in Chapter 9, Observer.

CHALLENGE 13.1Name a Swing class that provides an example of FLYWEIGHT.

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.