Flyweight by

Use sharing to support large numbers of fine-grained objects efficiently.

Motivation

Flyweight is a structural pattern - it is concerned with how classes and structures are composed to form a larger structure. This pattern describes a structure, which enables sharing reusable parts of an object through composition.

Objects using the flyweight pattern are made up of two parts. The shareable intrinsic state of the object is stored in a shared flyweight object and the non shareable context dependent extrinsic state is stored in client objects. The client object is responsible for passing extrinsic state to the flyweight object for operations. Having shared objects with intrinsic state is the essential part of flyweight pattern, the rest is optional based necessity.

Like the name implies, this is a very light pattern and also often used in conjunction with other patterns, such as command or state. For example we have already made use of the pattern during command tasks. Instead of creating new command for each execution and binding the receiver to the command, we removed the receiver as extrinsic state and passed it into the flyweight part through the execute method. This enabled us to reuse same commands through a dictionary for every consecutive execution, instead of creating new commands.

The effect of this pattern is a smaller memory footprint, as all of the intrinsic state is shared. You should apply this pattern when there is a large number of objects, most object state can be made extrinsic and groups of objects can be represented by few shared objects, once extrinsic state is removed. In some cases the extrinsic state could be computed - completely removing the need to store extrinsic state objects.

Unity 3D framework makes use of flyweight in several places. Its renderers, such as mesh renderer, which all share mesh objects and materials for rendering, instead of keeping multiple copies of the same objects in memory. The extrinsic state is kept in Mesh renderer component, along with few other non shared variables and the Mesh Filter only keeps a reference to the 3d mesh object, which are shared. Analogically you may find number of other components, which use rendering and physics materials, meshes, texture or sprites.

http://gameprogrammingpatterns.com/flyweight.html creates an elegant example of using flyweight to share terrain data in a game, while also achieving a very object orientented friendly access to the terrain data.

 

Structure

 

Participants

Flyweight

  • Declares an interface through which concrete flyweights can act on extrinsic states.

ConcreteFlyweight

  • Implements flyweight interface and stores intrinsic state, if any. These objects must be shareable

UnsharedConcreteFlyweight

  • Implements interface, but isn’t shareable. The patterns goal is to enable sharing, not to enforce it. Unshared flyweights often have a reference to a shared concrete flyweight object.

FlyweightFactory

  • Creates and manages flyweight objects
  • Could be unnecessary if we only need a set number of flyweights, which we set up during initialization.

Client

  • Maintains reference to flyweights
  • Computes or stores the extrinsic state of flyweight objects.

 

Example Uses

Use the Flyweight pattern when:

  • Application uses a large number of objects

  • Most of the object state can be made extrinsic

  • Groups of objects can be represented by a few shared objects, when extrinsic state is removed

  • The application doesn’t depend on objects identity. Since flyweight objects are shared, identity tests will return true for conceptually distinct objects.

;