Flyweight#

The Flyweight design pattern is a structural pattern that allows you to share common parts of objects to save memory. Instead of creating a large number of similar objects, you create a few shared objects (flyweights) and use them in multiple contexts.

Key Concepts#

  1. Flyweight: The shared object that can be used in multiple contexts.

  2. Context: The environment in which the flyweight is used.

  3. Flyweight Factory: Creates and manages flyweight objects.

UML Diagram#

+-----------------+        +-----------------+
|  Client         | --->>  |  Flyweight      |
+-----------------+        +-----------------+
                                ^
                                |
                                |
+-----------------+        +-----------------+
| FlyweightFactory|------->| ConcreteFlyweight|
+-----------------+        +-----------------+
--- Java Example ---
--- Python Example ---