© Dmitri Nesteruk 2020
D. NesterukDesign Patterns in .NET Core 3https://doi.org/10.1007/978-1-4842-6180-4_12

12. Flyweight

Dmitri Nesteruk1  
St. Petersburg, c.St-Petersburg, Russia

A Flyweight (also sometimes called a token or a cookie ) is a temporary component which acts as a “smart reference” to something. Typically, flyweights are used in situations where you have a very large number of very similar objects, and you want to minimize the amount of memory that is dedicated to storing all these values.

Let’s take a look at some scenarios where this pattern becomes relevant.

User Names

Imagine a massively multiplayer online game. I bet you $20 there’s more than one user called John Smith – quite simply because it is a popular name. So if we were ...

Get Design Patterns in .NET Core 3: Reusable Approaches in C# and F# for Object-Oriented Software Design now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.