Structural

Flyweight

Shares common state between multiple objects to reduce memory usage.

Text editorsGame developmentCaching systems

Understanding Flyweight

The Flyweight pattern minimizes memory usage by sharing common data between multiple objects. It separates intrinsic (shared) state from extrinsic (unique) state.

How It Works

Object 1
name, color, texture...
Object 2
name, color, texture...
Object 3
name, color, texture...
Object 4
name, color, texture...
Object 5
name, color, texture...
Object 6
name, color, texture...
Object 7
name, color, texture...
Object 8
name, color, texture...
Object 9
name, color, texture...
Object 10
name, color, texture...
Object 11
name, color, texture...
Object 12
name, color, texture...
⚠ High memory usage
1

Memory Problem

Creating many similar objects consumes lots of memory.

1 / 4

Basic Implementation

Forest rendering with shared tree type objects:

main.go
Loading editor...

Real-World Example: Text Editor

Character formatting with shared format objects:

main.go
Loading editor...