Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>The <a href="http://en.wikipedia.org/wiki/Flyweight_pattern">Flyweight Pattern</a> is a pattern for greatly reducing memory requirements by not requiring that heavy-weight objects be created in large numbers when dealing with systems that contain many things that are mostly the same. If for instance, a document was modeled using a complex character class that knew about unicode, fonts, positioning, etc., then the memory requirements could be quite large for large documents if each physical character in the document required its own character class instance. Instead, characters themselves might be kept within Strings and we might have one character class (or a small number such as one character class for each font type) that knew the specifics of how to deal with characters.</p> <p>In such circumstances, we call the state that is shared with many other things (e.g. the character type) <em>instrinsic</em> state. It is captured within the heavy-weight class. The state which distinguishes the physical character (maybe just its ASCII code or Unicode) is called its <em>extrinsic</em> state. </p> <h3>Example</h3> <p>First we are going to model some complex aircraft (the first being a hoax competitor of the second - not that is relevant to the example).</p> <table class="wysiwyg-macro" data-macro-name="section" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3NlY3Rpb259&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <table class="wysiwyg-macro" data-macro-name="column" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvbHVtbn0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> class Boeing797 { def wingspan = '80.8 m' def capacity = 1000 def speed = '1046 km/h' def range = '14400 km' // ... } </pre></td></tr></table> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> class Airbus380 { def wingspan = '79.8 m' def capacity = 555 def speed = '912 km/h' def range = '10370 km' // ... } </pre></td></tr></table></td></tr></table> <table class="wysiwyg-macro" data-macro-name="column" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvbHVtbn0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <p><img class="confluence-embedded-image" src="/download/attachments/231082398/b797-hoax.jpg?version=1&modificationDate=1371719830415" data-image-src="/download/attachments/231082398/b797-hoax.jpg?version=1&modificationDate=1371719830415" data-linked-resource-id="231377367" data-linked-resource-type="attachment" data-linked-resource-default-alias="b797-hoax.jpg" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231082398" title="null > b797-hoax.jpg"><br /> <br class="atl-forced-newline" /> <img class="confluence-embedded-image" src="/download/attachments/231082398/a380.jpg?version=1&modificationDate=1371719830424" data-image-src="/download/attachments/231082398/a380.jpg?version=1&modificationDate=1371719830424" data-linked-resource-id="231377368" data-linked-resource-type="attachment" data-linked-resource-default-alias="a380.jpg" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231082398" title="null > a380.jpg"></p></td></tr></table></td></tr></table> <p>If we want to model our fleet, our first attempt might involve using many instances of these heavy-weight objects. It turns out though that only a few small pieces of state (our extrinsic state) change for each aircraft, so we will have singletons for the heavy-weight objects and capture the extrinsic state (bought date and asset number in the code below) separately.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> class FlyweightFactory { static instances = [797: new Boeing797(), 380: new Airbus380()] } class Aircraft { private type // instrinsic state private assetNumber // extrinsic state private bought // extrinsic state Aircraft(typeCode, assetNumber, bought) { type = FlyweightFactory.instances[typeCode] this.assetNumber = assetNumber this.bought = bought } def describe() { println """ Asset Number: $assetNumber Capacity: $type.capacity people Speed: $type.speed Range: $type.range Bought: $bought """ } } def fleet = [ new Aircraft(380, 1001, '10-May-2007'), new Aircraft(380, 1002, '10-Nov-2007'), new Aircraft(797, 1003, '10-May-2008'), new Aircraft(797, 1004, '10-Nov-2008') ] fleet.each{ p -> p.describe() } </pre></td></tr></table> <p>So here, even if our fleet contained hundreds of planes, we would only have one heavy-weight object for each type of aircraft.</p> <p>As a further efficiency measure, we might use lazy creation of the flyweight objects rather than create the initial map up front as in the above example.</p> <p>Running this script results in:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Asset Number: 1001 Capacity: 555 people Speed: 912 km/h Range: 10370 km Bought: 10-May-2007 Asset Number: 1002 Capacity: 555 people Speed: 912 km/h Range: 10370 km Bought: 10-Nov-2007 Asset Number: 1003 Capacity: 1000 people Speed: 1046 km/h Range: 14400 km Bought: 10-May-2008 Asset Number: 1004 Capacity: 1000 people Speed: 1046 km/h Range: 14400 km Bought: 10-Nov-2008 </pre></td></tr></table>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced