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#
Flyweight: The shared object that can be used in multiple contexts.
Context: The environment in which the flyweight is used.
Flyweight Factory: Creates and manages flyweight objects.
UML Diagram#
+-----------------+ +-----------------+
| Client | --->> | Flyweight |
+-----------------+ +-----------------+
^
|
|
+-----------------+ +-----------------+
| FlyweightFactory|------->| ConcreteFlyweight|
+-----------------+ +-----------------+
--- Python Example ---