Flyweight Pattern

noun

say it: “FLY-wayt PAT-urn” /ˈflaɪweɪt ˈpætərn/

A design pattern that stores the data many objects share once per kind, in a shared, read-only object, so each object keeps only its own small part.

What is the Flyweight pattern?

The Flyweight pattern stores the data that is the same for a whole kind of object once, in one shared, read-only object, and lets every object of that kind point to it. A stadium map's 50,000 seats share three seat types, and each seat keeps only its row and number.

premium = ("premium", 90, "gold")
seats = [(row, number, premium) for row in range(1, 3) for number in range(1, 4)]
print(len(seats), len({id(seat[2]) for seat in seats}))  # 6 1

In a sentence

  • “The Flyweight pattern cut the festival map from about 40 MB to about 8, because 200,000 places now share three seat types.”

Level: Freelancer · More OOP terms at this level

Updated