O'Reilly logo

Swift 2 Design Patterns by Julien Lange

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The flyweight pattern

This pattern can be used when the system needs to deal with a large number of similar objects. Instead of creating each element one by one, this pattern permits you to reuse an object that shares the same data.

Roles

The flyweight pattern is used to reduce the memory and resource usage of complex models that contain many hundreds and thousands of similar objects by reducing the number of objects created. It tries to reuse similar existing objects or creates a new one when no match is found.

This pattern can be used when:

  • We need to manipulate a lot of small similar objects
  • The cost (the memory/execution time) of this manipulation is high

Design

The following class diagram represents the generic structure of the pattern:

Participants ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required