Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Flyweight pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Overview== The flyweight pattern is useful when dealing with a large number of objects that share simple repeated elements which would use a large amount of memory if they were individually embedded. It is common to hold shared data in external [[data structure]]s and pass it to the objects temporarily when they are used. A classic example are the data structures used representing characters in a [[word processor]]. Naively, each character in a document might have a [[glyph]] object containing its font outline, font metrics, and other formatting data. However, this would use hundreds or thousands of bytes of memory for each character. Instead, each character can have a [[reference (computer science)|reference]] to a glyph object shared by every instance of the same character in the document. This way, only the position of each character needs to be stored internally. As a result, flyweight objects can:<ref>{{Cite web|date=2019-01-28|title=Implementing Flyweight Patterns in Java|url=https://www.developer.com/design/implementing-flyweight-patterns-in-java/|access-date=2021-06-12|website=Developer.com|language=en-US}}</ref> * store ''intrinsic'' state that is invariant, context-independent and shareable (for example, the code of character 'A' in a given character set) * provide an interface for passing in ''extrinsic'' state that is variant, context-dependent and can't be shared (for example, the position of character 'A' in a text document) Clients can reuse <code>Flyweight</code> objects and pass in extrinsic state as necessary, reducing the number of physically created objects. === Structure === [[File:w3sDesign Flyweight Design Pattern UML.jpg|frame|none|A sample UML class and [[sequence diagram]] for the Flyweight design pattern.<ref>{{cite web|title=The Flyweight design pattern - Structure and Collaboration|url=http://w3sdesign.com/?gr=s06&ugr=struct|website=w3sDesign.com|access-date=2017-08-12}}</ref>]] The above [[UML]] [[class diagram]] shows: * the <code>Client</code> class, which uses the flyweight pattern *the <code>FlyweightFactory</code> class, which [[Factory class|creates and shares <code>Flyweight</code> objects]] * the <code>Flyweight</code> [[Interface (computing)|interface]], which takes in extrinsic state and performs an operation *the <code>Flyweight1</code> class, which implements <code>Flyweight</code> and stores intrinsic state The sequence diagram shows the following [[Runtime (program lifecycle phase)|run-time]] interactions: # The <code>Client</code> object calls <code>getFlyweight(key)</code> on the <code>FlyweightFactory</code>, which returns a <code>Flyweight1</code> object. # After calling <code>operation(extrinsicState)</code> on the returned <code>Flyweight1</code> object, the <code>Client</code> again calls <code>getFlyweight(key)</code> on the <code>FlyweightFactory</code>. # The <code>FlyweightFactory</code> returns the already-existing <code>Flyweight1</code> object.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)