Flyweight pattern
|
Flyweight is a software design pattern. When many objects must be manipulated and these cannot afford to have extraneous data, flyweight is appropriate.
In the flyweight pattern, the data has no pointers to the data type methods, because these would consume too much space. Instead, the subroutines are called directly. In some cases, flyweight inheritance is performed by "shift-in" and "shift-out" data markers as a higher-level operation cycles through an array of flyweight data.
One classic example of a flyweight pattern are the characters stored in a word processor. Each character represents an object that has a font face, font size, and other formatting data. As you can imagine, having a large document with this data structure would be very impactful on the memory footprint of the word processor. Moreover, since much of this data is repeated, there must be a way to reduce the footprint - and with the Flyweight pattern, there is! Each of the character objects would contain a reference to a separate formatting object which contains the required properties. This greatly reduces the memory footprint by combining all of the like-formatted characters into simpler objects that reference a single formatting object.
There is also a version of this pattern for working with XML structures.
External links
- XML Flyweight Design Pattern (http://xmlpatterns.com/FlyweightMain.shtml)
- Article "Make your apps fly - Implement Flyweight to improve performance (http://www.javaworld.com/javaworld/jw-07-2003/jw-0725-designpatterns.html)" by David Geary
- Article "Enhancing Web Application Performance with Caching (http://theserverside.com/articles/article.tss?l=Caching)" by Neal Ford
- Article "The Flyweight Pattern (http://codeproject.com/gen/design/testvalidators.asp)" by Alberto Bar-Noy
- Sample Chapter "C# Design Patterns: The Flyweight Pattern (http://awprofessional.com/articles/article.asp?p=31563)" by James W. Cooper
- Section "Flyweight Text Entry Fields (http://btl.usc.edu/rides/documentn/refMan/rf21_d.html)" from the RIDES Reference Manual by Allen Munro and Quentin A. Pizzini
- Description (http://c2.com/cgi/wiki?FlyweightPattern) from Portland's Pattern Repository
- Overview (http://dofactory.com/Patterns/PatternFlyweight.aspx)
- Sourdough Design (http://sourdough.phpee.com/index.php?node=18)
- Use sharing to support large numbers of fine grained objects efficiently (http://finucane.de/flyweigh.htm)
- Structural Patterns - Flyweight Pattern (http://www.allapplabs.com/java_design_patterns/flyweight_pattern.htm)
- Class::Flyweight - implement the flyweight pattern in OO perl (http://perlmonks.thepen.com/94783.html)
- ASP.NET Code Sample (http://www.ragingsmurf.com/code.aspx?key=I7U64DMGWM)
- Citations (http://citeseer.ist.psu.edu/cis?q=flyweight+pattern) from CiteSeerde:Fliegengewicht