Flyweight design pattern

Our next pattern is the Flyweight design pattern. It's very commonly used in computer graphics and the video game industry, but not so much in enterprise applications.

Description

Flyweight is a pattern which allows sharing the state of a heavy object between many instances of some type. Imagine that you have to create and store too many objects of some heavy type that are fundamentally equal. You'll run out of memory pretty quickly. This problem can be easily solved with the Flyweight pattern, with additional help of the Factory pattern. The factory is usually in charge of encapsulating object creation, as we saw previously.

Objectives

Thanks to the Flyweight pattern, we can share all possible states of objects in a single ...

Get Go Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.