Use sharing to support large numbers of fine-grained objects efficiently.
You need to create a large number of similar objects, which causes memory issues due to the overhead of object creation.
The Flyweight pattern minimizes memory usage by sharing common parts of state between multiple objects instead of keeping all data in each object.
- An application uses a large number of objects
- Storage costs are high because of the quantity of objects
- Most object state can be made extrinsic
- Many groups of objects may be replaced by relatively few shared objects
- The application doesn't depend on object identity
See the Program.cs file for a forest simulation sharing tree types to reduce memory usage.
- Can save lots of RAM when dealing with many similar objects
- Centralizes state management for similar objects
- Code becomes more complicated
- Trading RAM for CPU cycles when some context data needs to be recalculated
- Extrinsic state must be computed or looked up