Flyweight Pattern


  • The Flyweight pattern reduces the cost of working with large numbers of very small objects.
  • The Flyweight pattern is specifically designed to be used when all the following conditions are in place:
  • There is a very large number of objects (thousands) that may not fit in memory.
  • Most of the state can be kept on disk or calculated at runtime.
  • The remaining state can be factored into a much smaller number of objects with shared state.

Class diagram: (see Flyweight Pattern code review)

Flyweight Pattern class diagram of sample code download


Use the Flyweight pattern when there are:

  • Many objects to deal with in memory.
  • Different kinds of state, which can be handled differently to achieve space savings.
  • Groups of objects that share state.
  • Ways of computing some of the state at runtime.