Flyweight pattern
Encyclopedia
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
.
External links
Design pattern (computer science)
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...
. 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
Flyweight
Flyweight is a class in boxing which includes fighters weighing less than 112 lb but above 108 lb .-Professional boxing:...
. 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
Word processor
A word processor is a computer application used for the production of any sort of printable material....
. It might be desirable to have, for each character in a document, a glyph
Glyph
A glyph is an element of writing: an individual mark on a written medium that contributes to the meaning of what is written. A glyph is made up of one or more graphemes....
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
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...
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
Hash consing
In computer science, particularly in functional programming, hash consing is a technique used to share values that are structurally equal. The term hash consing originates from implementations of Lisp that attempt to reuse cons cells that have been constructed before, avoiding the penalty of memory...
.
Example (java)
External links
- Flyweight in UML and in LePUS3 (a formal modelling language)
- Article "Enhancing Web Application Performance with Caching" by Neal Ford
- Section "Flyweight Text Entry Fields (archive.org)" from the RIDES Reference Manual by Allen Munro and Quentin A. Pizzini
- Description from Portland's Pattern Repository
- Sourdough Design
- Class::Flyweight - implement the flyweight pattern in OO perl
- Boost.Flyweight - A generic C++ implementation