Flyweight pattern

The Flyweight pattern is used for sharing set of predefined objects between clients.

advantages

  1. Saving memory.

flyweight vs object pool

  1. Flyweight objects are immutable, but object pool items are mutable.
  2. 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.
  3. 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.
  4. 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.