The Flyweight Pattern belongs to the Structural category.
Why? Because it helps you save memory by sharing common parts of objects instead of creating duplicates.
Think about a hotel with many rooms. All these rooms need chairs, tables, and lamps. Instead of buying unique furniture for every room, which is costly and takes up variable space in each room, the hotel buys a limited number of identical furniture pieces and reuses them across many rooms.
This sharing saves money and space, while each room still keeps its own layout and style (like where the furniture is placed or extra decorations).
The Text Editor
Suppose you are building a Text Editor app that handles large documents.
Each character in the document is an object with properties like font, size, and color.
If you create a separate object for every character including its style properties, your app will quickly consume a lot of memory.
So, the challenge here is, How do you reduce memory usage when you have many similar objects (characters with same style) repeated throughout the document?
Enter the Flyweight Pattern
The Flyweight Pattern lets you share common parts of objects (called intrinsic state) and store only unique parts (called extrinsic state) separately.
This reduces memory usage by reusing shared data instead of duplicating it.
What does the code look like?
// Step 1: Define the Character interface
Interface Character
Method Display(positionX, positionY)
// Step 2: Concrete Character Class (Flyweight)
Class ConcreteCharacter implements Character
Property symbol // intrinsic state, shared
Property font
Property size
Property color
Constructor(symbol, font, size, color)
Set properties
Method Display(positionX, positionY)
Print "Displaying '" + symbol + "' at ("
+ positionX + "," + positionY + ") with font="
+ font + ", size=" + size + ", color=" + color
// Step 3: Flyweight Factory to manage shared objects
Class CharacterFactory
Property characters = Dictionary of (symbol+font+size+color)
-> ConcreteCharacter
Method GetCharacter(symbol, font, size, color)
key = symbol + font + size + color
If characters contains key Then
Return characters[key]
Else
character = new ConcreteCharacter(symbol, font, size, color)
characters[key] = character
Return character
//caller logic (text editor)
factory = new CharacterFactory()
// For each character in document
charA1 = factory.GetCharacter('A', 'Arial', 12, 'Black')
// Reuses same object as charA1
charA2 = factory.GetCharacter('A', 'Arial', 12, 'Black')
charB = factory.GetCharacter('B', 'Arial', 12, 'Black')
charA1.Display(10, 20)
charA2.Display(15, 20)
charB.Display(20, 20)
The CharacterFactory
ensures that characters with the same intrinsic properties (symbol, font, size, color) are shared i.e. only one object is created and reused.
The unique part, like position, is passed as extrinsic state during method calls.
What Did We Achieve?
- Saved memory by sharing common parts of objects.
- Avoided creating many duplicate objects with the same properties.
- Kept unique parts separate by passing them externally when needed.
When Should You Use the Flyweight Pattern?
- When you have many similar objects that differ only in a few properties.
- When you want to optimize memory usage in your app.
- When object creation is costly or you want to reduce resource consumption.
Use Cases?
- Text editors handling lots of characters.
- Graphical apps rendering many shapes with shared properties.
- Game development where many objects share textures or models.
- Caching and object pooling.
Does the Flyweight Pattern look similar to the Prototype pattern?
Yes. It is often confused with the Prototype pattern, but let's understand the difference,
Flyweight Pattern (Structural): It focuses on sharing common parts of objects (called intrinsic state) to save memory when many similar objects exist. Instead of creating many full objects every time needed, it reuses shared data and passes unique information separately. This is great for optimizing resource use when you have lots of similar objects with small differences.
Prototype Pattern (Creational): It is about creating new objects by cloning an existing object (a prototype). It allows you to quickly generate copies with the same state as the original, which can then be modified independently. This is useful when object creation is costly or complex, and you want to create new objects efficiently by copying.
To summarize, it would be apt to say,
The Flyweight Pattern is like furnishing hotel rooms by sharing furniture i.e. saving memory and resources while still allowing each room to have unique arrangements.
Hope this made the Flyweight Pattern easy to understand!
Next up: Bridge Pattern. Please come over!
Top comments (0)