Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Flyweight pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Flyweight pattern
(section)
Add topic