The Flyweight pattern is used for sharing set of predefined objects between clients.
- Saving memory.
flyweight vs object pool
- Flyweight objects are immutable, but object pool items are mutable.
- From the first it follows that the flyweight object can be used by many clients at the same time, and an item from the object pool can only be used by one client.
- Usually flyweight objects differ in state, for example set of sprites based on one the image file but with differenet colors. And with the object pool item you want control some resource such database connection.
- Usually, when there are no free items in the object pool, it creates a new object for the client. Therefore, the object pool is a creational pattern. But with flyweight pattern, clients use predefined objects. Therefore, it is structural pattern.