DEV Community

Shirin Monzavi
Shirin Monzavi

Posted on

Memory management with Flyweight Design Pattern

๐Ÿš€ ๐’๐ฆ๐š๐ซ๐ญ ๐Œ๐ž๐ฆ๐จ๐ซ๐ฒ ๐Œ๐š๐ง๐š๐ ๐ž๐ฆ๐ž๐ง๐ญ ๐ฐ๐ข๐ญ๐ก ๐ญ๐ก๐ž ๐…๐ฅ๐ฒ๐ฐ๐ž๐ข๐ ๐ก๐ญ ๐๐š๐ญ๐ญ๐ž๐ซ๐ง ๐Ÿš€

โ‰๏ธ ๐‘ด๐’๐’•๐’Š๐’—๐’‚๐’•๐’Š๐’๐’
The intent of flyweight is to share an object instead of having many objects.

๐ŸŒ ๐‘น๐’†๐’‚๐’-๐‘พ๐’๐’“๐’๐’… ๐‘บ๐’„๐’†๐’๐’‚๐’“๐’Š๐’
Assume you want to design a document editor, and this editor contains:
โ€ข ๐Ÿ“Š Tables
โ€ข ๐Ÿ–ผ๏ธ Figures
โ€ข ๐Ÿ”ค Characters

In object-oriented design, we usually represent these entities as objects.
โš ๏ธ But here's the problem:
Documents can contain thousands of characters, and creating a separate object for each one leads to:
๐Ÿ”ป Too many objects
๐Ÿ”ป High memory usage
๐Ÿ”ป Unacceptable run-time overhead

๐Ÿ’ก This is where the Flyweight Pattern becomes useful โ€” it helps avoid the cost of creating tons of similar objects by sharing them efficiently.

๐ŸŽฏ ๐’๐จ๐ฅ๐ฎ๐ญ๐ข๐จ๐ง โ€” ๐…๐‹๐˜๐–๐„๐ˆ๐†๐‡๐“ ๐๐š๐ญ๐ญ๐ž๐ซ๐ง
Flyweight is an object which can be shared in multiple contexts simultaneously.

๐Ÿ”‘ Flyweights cannot guess the context they operate in, so the client must pass the extrinsic state when needed.

๐Ÿง  The key idea lies in the difference between:
๐Ÿ”ธ Intrinsic state: stored inside the flyweight โ€” it's independent of the context and sharable.
๐Ÿ”ธ Extrinsic state: provided by the client โ€” it's context-dependent and cannot be shared.

Image description

๐Ÿ“Œ Example:
The character 'A' in a document is an intrinsic part โ€” the alphabet symbol stays the same.
But the font size, color, and position are extrinsic, as they vary by context.

๐Ÿง  ๐€๐ฉ๐ฉ๐ฅ๐ฒ ๐…๐ฅ๐ฒ๐–๐ž๐ข๐ ๐ก๐ญ ๐ฐ๐ก๐ž๐ง ๐š๐ฅ๐ฅ ๐จ๐Ÿ ๐ญ๐ก๐ž ๐Ÿ๐จ๐ฅ๐ฅ๐จ๐ฐ๐ข๐ง๐  ๐š๐ซ๐ž ๐ญ๐ซ๐ฎ๐ž:
โœ… A program uses a lot of objects.
โœ… Storage costs are high due to the sheer quantity of objects.
โœ… Most object state can be made extrinsic.
โœ… Removing extrinsic state allows many objects to be replaced with fewer shared ones.
โœ… The application does not rely on object identity. Identity check may return true even if the instances of flyweight are different conceptually. (e.g., font, size, color).

๐Ÿ’Ž ๐‘น๐’†๐’๐’‚๐’•๐’†๐’… ๐‘ท๐’‚๐’•๐’•๐’†๐’“๐’๐’”:
โ—พ ๐‚๐จ๐ฆ๐ฉ๐จ๐ฌ๐ข๐ญ๐ž: The Flyweight pattern is often combined with the Composite pattern to implement a logically hierarchical structure in terms of a directed-acyclic graph with shared leaf nodes.

โ—พIt's often best to implement ๐’๐ญ๐š๐ญ๐ž and ๐’๐ญ๐ซ๐š๐ญ๐ž๐ ๐ฒ objects as flyweights.

๐Ÿ“‚ ๐‚๐จ๐๐ž ๐„๐ฑ๐š๐ฆ๐ฉ๐ฅ๐ž
๐Ÿ‘‰ GitHub - https://github.com/shirin-monzavi/FlyWeightDesignPattern

โ“Have you ever used the ๐…๐‹๐˜๐–๐„๐ˆ๐†๐‡๐“ Pattern in your projects?

Top comments (0)