🎨 Design Patterns (Gang of Four)
Beginner
What is the Flyweight design pattern?
Answer
The Flyweight pattern reduces memory usage by sharing as much data as possible with similar objects. It separates an object's state into intrinsic (shared, immutable, stored in the flyweight) and extrinsic (unique per context, passed in at runtime) state. A classic example is a text editor that renders millions of characters — instead of creating a separate object for every 'e' on the page, one flyweight object represents the letter 'e' and its position (extrinsic) is passed in when rendering. Java's String.intern() and integer caching (-128 to 127) use this principle.