Flyweight pattern

related topics
{math, number, function}
{system, computer, user}
{style, bgcolor, rowspan}
{school, student, university}
{work, book, publish}

Flyweight is a software design pattern. A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory. The term is named after the boxing weight class. Often some parts of the object state can be shared and it's common to put them in external data structures and pass them to the flyweight objects temporarily when they are used.

A classic example usage of the flyweight pattern is the data structures for graphical representation of characters in a word processor. It might be desirable to have, for each character in a document, a glyph object containing its font outline, font metrics, and other formatting data, but this would amount to hundreds or thousands of bytes for each character. Instead, for every character there might be a reference to a flyweight glyph object shared by every instance of the same character in the document; only the position of each character (in the document and/or the page) would need to be stored internally.

In other contexts the idea of sharing identical data structures is called hash consing.


The following programs illustrate the document example given above: the flyweights are called FontData in the Java example.

The examples illustrate the flyweight pattern used to reduce memory by loading only the data necessary to perform some immediate task from a large Font object into a much smaller FontData (flyweight) object.

Full article ▸

related documents
ActiveX Data Objects
Specification language
8.3 filename
Java Naming and Directory Interface
Literate programming
Elias gamma coding
Earley parser
Axiom of power set
Injective function
Unitary matrix
Parse tree
Unit interval
Linear function
Inner automorphism
Regular graph
Euler's identity
Disjunctive normal form
Profinite group
Inverse transform sampling
Class (set theory)
Discrete probability distribution
Hausdorff maximal principle
Row and column spaces