04 Dec 2023

Flyweight

Flyweight lets you for more objects into the available amount of RAM by sharing common parts between multiple objects.

For example, in text editors, each character in a document can be represented as a flyweight object. The character's intrinsic state (such as its Unicode code point) is shared among all instances, while the extrinsic state (e.g., position and formatting) is stored separately for each character.

Cons

  • You might be trading RAM over CPU cycles when some of the data needs to be recalculated each time.
  • The code can become much more complicated