Pattern · Advanced

Flyweight

When you need a HUGE number of similar objects, share their common heavy data instead of duplicating it — keep only the per-object differences.

Asked atAmazonGoogleUnity
step 1 / 7
1/2A million heavy objects
Tree
- x, y
- mesh (2 MB)
- texture (3 MB)
A million heavy objects
1class Tree {
2 int x, y;
3 Mesh mesh; // 2 MB
4 Texture texture; // 3 MB — copied into EVERY tree
5}

Each Tree carries its own mesh and texture — 5 MB of heavy geometry baked right into the object alongside its x, y.