Flyweight (Chapter 13)

SOLUTION 13.1The BorderFactory class in javax.swing is a good example of FLYWEIGHT. As its class comment says, “Wherever possible, this factory will hand out references to shared Border instances.” When a Border object can be safely shared, BorderFactory will return the same border more than once. For example, every call to createEmptyBorder() will return the same, empty border.
SOLUTION 13.2
  • An argument for the immutability of strings: In practice, strings are frequently shared between clients and thus frequently the crux of defects that emerge when one client inadvertently affects another. For example, a method that returns a customer's name as a string will typically retain its reference to the name. If the client, say, ...

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.