<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ActionScript 3.0 Design Patterns &#187; Flyweight</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/flyweight/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:00:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The Flyweight Design Pattern: Where Shared Objects Solve Storage Problems</title>
		<link>http://www.as3dp.com/2007/11/the-flyweight-design-pattern-where-shared-objects-solve-storage-problems/</link>
		<comments>http://www.as3dp.com/2007/11/the-flyweight-design-pattern-where-shared-objects-solve-storage-problems/#comments</comments>
		<pubDate>Mon, 26 Nov 2007 17:01:38 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flyweight]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2007/11/26/the-flyweight-design-pattern-where-shared-objects-solve-storage-problems/</guid>
		<description><![CDATA[Note: After three Flyweight Saga entries, I think I have all of the parts explained and working like they should. To double-check, the following was presented at the 2007 OOPSLA conference in Montreal. The great comments that the readers of this blog provided were most helpful, and further comments by those in the OOPSLA Killer [...]]]></description>
			<content:encoded><![CDATA[<p><strong><em>Note:</em></strong><em> After three Flyweight Saga entries, I think I have all of the parts explained and working like they should. To double-check, the following was presented at the 2007 OOPSLA conference in Montreal. The great comments that the readers of this blog provided were most helpful, and further comments by those in the OOPSLA Killer Examples session all added to what I hope works to clarify using the Flyweight Design Pattern with ActionScript 3.0. Also, everything in this article is based on Design Patterns Elements of Reusable Object-Oriented Software by the Gamma, et al.</em></p>
<p>Air traffic controllers look at virtual simulations of hundreds of aircraft. The images on a screen give the ATCs the information they need for separating the many planes under their control. The hub airports such as Chicago O’Hare and Dallas-Ft. Worth have to juggle hundreds of simultaneous flights arriving at and departing from their respective airports. To maintain accuracy, images that display position, heading, altitude, level flight, ascent and descent must be updated frequently, accurately and quickly. In looking over the set of design patterns set forth by Gamma, et al, the Flyweight pattern offers the following:</p>
<ul>
<li> 	An application uses a large number of objects</li>
<li>Storage costs are high because of the sheer quantity of objects</li>
<li>Most object state can be made extrinsic</li>
<li>Many groups of objects may be replaced by relatively few shared objects once extrinsic state is removed</li>
<li>The application doesn’t depend on object identity. Since flyweight objects may be shared, identity tests will return true for conceptually distinct objects</li>
</ul>
<p>That pretty well fills the bill for what is required. We need a large number of objects (airplane images), the sheer quantity has high storage costs, the extrinsic state such as heading, horizontal position and vertical position can be made extrinsic, once extrinsic state is removed, a few objects can be used to represent groups of objects, and the application is not dependent of object identity. This is not to say that one cannot be distinguished from the other, but rather the object identity is relative to its extrinsic characteristics.Figure 1 shows the basic class diagram.</p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2007/11/flyweightdiagram.gif"><img src="http://www.as3dp.com/wp-content/uploads/2007/11/flyweightdiagram.gif" height="353" width="527" /><br />
</a><strong>Figure 1: Class Diagram</strong></p>
<p>The key elements of the class include the following:</p>
<ul>
<li> 	The Flyweight class, (an abstract class or interface) with the operation with parameters for the extrinsic states.</li>
<li>A Flyweight Factory that aggregates the Flyweight class. (The ball at the end of the aggregation arrow indicates that multiple aggregations may exist.)</li>
<li>A Concrete Flyweight contains the extrinsic states and the intrinsic states. This is a shared object</li>
<li>(Optionally) An unshared concrete Flyweight containing the extrinsic state but cannot be shared.</li>
<li>A Client participant. The Client class in this design pattern has a responsibility and has acquaintance relations with both the concrete flyweights and the factory classes.</li>
</ul>
<p>
  <span id="more-39"></span><br />
  The primary functionality of the Flyweight design pattern is the ability to quickly make multiple copies of an object by creating one instance and sharing it for multiple re-uses. The instances and reference to the instances are generated in a Flyweight Factory that aggregates the Flyweight interface or abstract class. The intrinsic value shared is stored in a concrete Flyweight and then shared for multiple uses by assigning different extrinsic values. A Flyweight shared object is one with a common intrinsic value that is used by all references to the Flyweight. The extrinsic values provide the shared object with contextual values that are not shared.</p>
<p><strong>Aggregation and Acquaintance</strong></p>
<p>A key relationship between the Flyweight and Flyweight Factory is that of aggregation. The factory participant is the aggregator and the flyweight the aggregatee. The nature of the aggregation relationship is unique in that the aggregator holds a dependent reference in the sense that its life is dependent on the life of the aggregatee. The following shows what such a relationship looks like at the point where it occurs in the code:</p>
<p><code>public function getIntrinsic (key:String,... rest):FlyweightPlane</code></p>
<p>In this case assigning the return type as FlyweightPlane, the Flyweight interface or abstract class, makes the aggregation. The aggregate can only work as long as the FlyweightPlane is extant, and so stands as an example of aggregation in the sense that that have identical lifetimes.The Client participant is connected to the other participants through acquaintance. It has an acquaintance with the Factory and Concrete Client[s]. For instance, declaring an instance of the Factory in the Client participant establishes an acquaintance:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code21'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3921"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p39code21"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> planeFactory;
…
planeFactory=<span style="color: #000000; font-weight: bold;">new</span> PlaneFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
plane1=planeFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;east&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Likewise, the Client participant establishes an acquaintance with the Concrete Flyweight, but far more indirectly. For example, the line,</p>
<p><code>plane1.doPlane (xPos,yPos,w,h,curve);</code></p>
<p>creates an acquaintance through the PlaneFactory aggregation of the Flyweight and its subclass, a Concrete Flyweight. In this case, the acquaintance is through the key Flyweight operation (doPlane) implemented in the Concrete Flyweight.</p>
<p><strong>Intrinsic and Extrinsic States</strong></p>
<p>Another defining characteristic of the Flyweight is the handling of intrinsic and extrinsic states. An intrinsic state is immutable and cannot be changed, and as such can be used by other references to the same instance. The intrinsic state is stored in the Concrete Flyweight. For example, a car has an engine (intrinsic state) however the car has many different contexts (extrinsic state), such as it’s location, whether it is moving or stopped and other states that cannot be shared (like the same parking place).The Flyweight establishes an operation to specify the possible extrinsic states as parameters. The Client participant is responsible for supplying the extrinsic state. For example, the line,</p>
<p><code>plane1.doPlane (xPos,yPos,w,h,curve);</code></p>
<p>requires values for five extrinsic states. The horizontal and vertical position of the sprite, its width and height and the curve value of a rounded rectangle are the included extrinsic parameters. The Following line</p>
<p><code>plane1.doPlane (134,220,60,10,20);</code></p>
<p>draws a button-shaped rectangle positioned at:</p>
<ul>
<li> 	horizontal position = 134</li>
<li>vertical position = 220</li>
<li>rounded rectangle width = 50</li>
<li>rounded rectangle height = 10</li>
<li>rounded rectangle curve=20</li>
</ul>
<p>These values are all extrinsic states of the object provided by the client.An intrinsic state can be anything sharable. For example, in the following examples, the intrinsic value is the color. All of the objects have the same color even though the instance references have different horizontal and vertical positions.</p>
<p><strong>The Flyweight Factory</strong></p>
<p>Creating multiple references to the same instance is possible because of the Flyweight Factory. The basis of the Flyweight Factory is its ability to recognize an extant instance and re-use it by adding it to an associative array (or hash table). This has the effect of re-using the same object’s intrinsic state by adding it to the array. In most discussions of the Flyweight Factory, hash tables are used. Both C# and Java, for example, use hash tables. ActionScript 3.0 has no hash tables so associative arrays used to effect equivalent results. Associative arrays are built using the Object class rather than the Array class. (None of the Array class methods or properties are available when keys are used instead of numeric indices.)</p>
<p><strong><em>Shared Object</em></strong></p>
<p>With the creation of a new element of an associative array each key corresponds to a property name. The associated value and name establish a key and value pair. If the key is undefined, the associative array creates a new one. The following script shows a generic example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code22'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3922"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p39code22"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Factory
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Associative Array</span>
 protected <span style="color: #000000; font-weight: bold;">var</span> storeFlyweight:<span style="color: #0066CC;">Object</span>=<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Factory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">&#123;</span>
  getFlyweights <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
 <span style="color: #66cc66;">&#125;</span>
&nbsp;
 <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getFlyweights <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">&#123;</span>
  storeFlyweight<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;first&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> ConcreteFlyweight<span style="color: #66cc66;">&#40;</span>intrinsic<span style="color: #66cc66;">&#41;</span>;
  storeFlyweight<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;second&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> ConcreteFlyweight<span style="color: #66cc66;">&#40;</span>intrinsic<span style="color: #66cc66;">&#41;</span>;
 <span style="color: #66cc66;">&#125;</span>
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getIntrinsic <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span>,... <span style="color: #006600;">rest</span><span style="color: #66cc66;">&#41;</span>:Flyweight
 <span style="color: #66cc66;">&#123;</span>
  <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>storeFlyweight<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">true</span> :
    <span style="color: #b1b100;">break</span>;
   <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">false</span> :
    storeFlyweight <span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>=rest<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
    <span style="color: #b1b100;">break</span>;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #b1b100;">return</span> storeFlyweight <span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>;
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong><em>Unshared Objects</em></strong></p>
<p>As can be seen, the Flyweight Factory is pretty much like any other factory class except that it returns multiple copies of a single instance. In other words, it’s a shared object. In looking at Factory Method factories, you will not see this. For instance, the following example by Chandima Cumaranatunge (Sanders and Cumaranatunge, 2007) is for a simple factory and it deals with two different instances of an object:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code23'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3923"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p39code23"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Creator
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> simpleFactory<span style="color: #66cc66;">&#40;</span>product:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>product == <span style="color: #ff0000;">&quot;p1&quot;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> product1<span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>product == <span style="color: #ff0000;">&quot;p2&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> product2<span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The result is that more objects are created resulting in more elements to be stored and displayed. Thus, rather than one instance, the results are multiple instances.</p>
<p><strong>A Simple Flyweight Example: Making Balls</strong></p>
<p>To get started, it’s best to start with a minimalist example so that all of the participants can be seen. In this set of examples, the focus is on the following participants:</p>
<ul>
<li> 	Flyweight</li>
<li>Flyweight Factory</li>
<li>Concrete Flyweight (Shared)</li>
<li>Client</li>
</ul>
<p>The Unshared Concrete Flyweight is not included since it is optional and at this level really is unneeded.The example does nothing more than create several instances of a ball object. Four of the balls are created using the Flyweight Factory and one is created by simply instantiating an instance of the ball object (Green). The unshared object is to illustrate that there’s no discernable difference in appearance of shared or unshared objects.</p>
<p><strong><em>Abstract Flyweight</em></strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code24'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3924"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p39code24"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Abstract class Flyweight</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyweightBall <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> doCircle <span style="color: #66cc66;">&#40;</span>xPos:uint,yPos:uint,radius:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Flyweight Factory</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code25'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3925"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p39code25"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Flyweight Factory</span>
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BallFactory
 <span style="color: #66cc66;">&#123;</span>
  protected <span style="color: #000000; font-weight: bold;">var</span> ballIntrinsic:<span style="color: #0066CC;">Object</span>=<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BallFactory <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> FlyBall<span style="color: #66cc66;">&#40;</span>0x990000<span style="color: #66cc66;">&#41;</span>;
   ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;yellow&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> FlyBall<span style="color: #66cc66;">&#40;</span>0xffff00<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getIntrinsic <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span>,... <span style="color: #006600;">rest</span><span style="color: #66cc66;">&#41;</span>:FlyweightBall
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">true</span> :
     <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">false</span> :
     ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>=rest<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
     <span style="color: #b1b100;">break</span>;
   <span style="color: #66cc66;">&#125;</span>
   <span style="color: #b1b100;">return</span> ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Concrete Flyweight</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code26'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3926"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p39code26"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Concrete Flyweight</span>
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyBall <span style="color: #0066CC;">extends</span> FlyweightBall
 <span style="color: #66cc66;">&#123;</span>
  protected <span style="color: #000000; font-weight: bold;">var</span> fill: uint;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FlyBall <span style="color: #66cc66;">&#40;</span>fill:uint<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #0066CC;">this</span>.<span style="color: #006600;">fill</span>=fill;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  override <span style="color: #000000; font-weight: bold;">function</span> doCircle <span style="color: #66cc66;">&#40;</span>xPos:uint,yPos:uint,radius:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   graphics.<span style="color: #0066CC;">beginFill</span> <span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #006600;">drawCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #0066CC;">endFill</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Client</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code27'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3927"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
</pre></td><td class="code" id="p39code27"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Client class</span>
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BallClient <span style="color: #0066CC;">extends</span> Sprite
 <span style="color: #66cc66;">&#123;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">//Client data to provide extrinsic state details</span>
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> xPos:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> yPos:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> radius:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cir1:FlyweightBall;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cir2:FlyweightBall;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cir3:FlyweightBall;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cir4:FlyweightBall;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cir5:FlyBall;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BallClient <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">var</span> ballFactory:BallFactory=<span style="color: #000000; font-weight: bold;">new</span> BallFactory;
&nbsp;
   cir1=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#41;</span>;
   shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   cir1.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
   addChild <span style="color: #66cc66;">&#40;</span>cir1<span style="color: #66cc66;">&#41;</span>;
&nbsp;
   cir2=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#41;</span>;
   shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   cir2.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
   addChild <span style="color: #66cc66;">&#40;</span>cir2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
   cir3=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yellow&quot;</span><span style="color: #66cc66;">&#41;</span>;
   shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   cir3.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
   addChild <span style="color: #66cc66;">&#40;</span>cir3<span style="color: #66cc66;">&#41;</span>;
&nbsp;
   cir4=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yellow&quot;</span><span style="color: #66cc66;">&#41;</span>;
   shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   cir4.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
   addChild <span style="color: #66cc66;">&#40;</span>cir4<span style="color: #66cc66;">&#41;</span>;
&nbsp;
   <span style="color: #808080; font-style: italic;">//Unshared--Not a Flyweight instance</span>
   cir5=<span style="color: #000000; font-weight: bold;">new</span> FlyBall<span style="color: #66cc66;">&#40;</span>0x009900<span style="color: #66cc66;">&#41;</span>;
   shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   cir5.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
   addChild <span style="color: #66cc66;">&#40;</span>cir5<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #808080; font-style: italic;">//Generate extrinsic states</span>
   xPos=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span>;
   yPos=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span>;
   radius=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Figure 2 shows a typical output. (Because the radii and Cartesian coordinates are random, the output is different each time the application launches and what you see may be entirely different.)</p>
<p>The Flyweight’s only intrinsic value is the color, and when a Flyweight is created in the Factory, it is done so providing a color value. The name of the object is stored in the associative array, and when another request is made using the same name, the instance is shared with the new request. The extrinsic value are all provided by the Client using random values.</p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2007/11/balls.gif"><img src="http://www.as3dp.com/wp-content/uploads/2007/11/balls.gif" /><br />
</a><strong>Figure 2: Four Flyweights and an Unshared Green Object</strong><strong>Showing Single Instance with Multiple Elements</strong></p>
<p>The problem with the ball example is that it is a <em>take my word for it</em> example. That is, it demonstrates nothing to convince or show that the two red and the two yellow balls are any different from the green one. It is useful insofar as the code shows that the green ball is not created in the same way as the others, but the consequences of that are not too clear.</p>
<p><strong><em>Beyond Extrinsic: Instance External Properties</em></strong></p>
<p>The way to show that a single instance contains multiple uses of a single object is to change a property of the instance to have an effect on all instances in the object. The extrinsic values can change the horizontal and vertical positions of the balls as well as the radius of each ball. So, while the two yellow balls in Figure 2 have different positions, they are still part of a single instance of the ball.One property that is not part of the extrinsic parameters is rotation. However, rotating a ball isn’t much help because all rotation angles look the same. So, now would be a good time to start building the air traffic control image representing a single aircraft. Thus, instead of a ball, this next example will use an airplane and different extrinsic values. However, rotation will not be one of these extrinsic values. That will be saved for the entire instance. The following application makes these adjustments.</p>
<p><strong>Abstract Flyweight</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code28'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3928"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p39code28"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Abstract class Flyweight</span>
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyweightPlane <span style="color: #0066CC;">extends</span> Sprite
 <span style="color: #66cc66;">&#123;</span>
 	<span style="color: #000000; font-weight: bold;">function</span> doPlane <span style="color: #66cc66;">&#40;</span>xPos:uint,yPos:uint,w:uint,h:uint,curve:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Flyweight Factory</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code29'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3929"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p39code29"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Flyweight Factory</span>
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PlaneFactory
 <span style="color: #66cc66;">&#123;</span>
  protected <span style="color: #000000; font-weight: bold;">var</span> rectIntrinsic:<span style="color: #0066CC;">Object</span>=<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PlaneFactory <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   rectIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> MakePlane<span style="color: #66cc66;">&#40;</span>0x990000<span style="color: #66cc66;">&#41;</span>;
   rectIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;green&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> MakePlane<span style="color: #66cc66;">&#40;</span>0x009900<span style="color: #66cc66;">&#41;</span>;
   rectIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> MakePlane<span style="color: #66cc66;">&#40;</span>0x000099<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getIntrinsic <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span>,... <span style="color: #006600;">rest</span><span style="color: #66cc66;">&#41;</span>:FlyweightPlane
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>rectIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">true</span> :
     <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">false</span> :
     rectIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>=rest<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
     <span style="color: #b1b100;">break</span>;
   <span style="color: #66cc66;">&#125;</span>
   <span style="color: #b1b100;">return</span> rectIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Concrete Flyweight</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code30'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3930"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p39code30"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Concrete Flyweight</span>
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakePlane <span style="color: #0066CC;">extends</span> FlyweightPlane
 <span style="color: #66cc66;">&#123;</span>
  protected <span style="color: #000000; font-weight: bold;">var</span> fill: uint;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MakePlane <span style="color: #66cc66;">&#40;</span>fill:uint<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #0066CC;">this</span>.<span style="color: #006600;">fill</span>=fill;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  override <span style="color: #000000; font-weight: bold;">function</span> doPlane <span style="color: #66cc66;">&#40;</span>xPos:uint,yPos:uint,w:uint,h:uint,curve:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   graphics.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>
   graphics.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span>xPos-<span style="color: #cc66cc;">18</span>,yPos+<span style="color: #cc66cc;">17</span>,<span style="color: #cc66cc;">45</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span>
   graphics.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span>xPos-<span style="color: #cc66cc;">7</span>,yPos+<span style="color: #cc66cc;">37</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>
   graphics.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Client</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code31'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3931"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
</pre></td><td class="code" id="p39code31"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Client class</span>
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PlaneClient <span style="color: #0066CC;">extends</span> Sprite
 <span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">//Client data to provide extrinsic state details</span>
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> xPos:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> yPos:uint=<span style="color: #cc66cc;">0</span>;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> w:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> h:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> curve:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> planeFactory:PlaneFactory;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> plane1:FlyweightPlane;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> plane2:FlyweightPlane;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> plane3:FlyweightPlane;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> plane4:FlyweightPlane;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> plane5:FlyweightPlane;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> plane6:FlyweightPlane;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PlaneClient <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   planeFactory=<span style="color: #000000; font-weight: bold;">new</span> PlaneFactory ;
&nbsp;
   plane1=planeFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#41;</span>;
   place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   plane1.<span style="color: #006600;">doPlane</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>;
   addChild <span style="color: #66cc66;">&#40;</span>plane1<span style="color: #66cc66;">&#41;</span>;
   plane1.<span style="color: #006600;">rotation</span>=<span style="color: #cc66cc;">15</span>;
&nbsp;
   plane2=planeFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#41;</span>;
   place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   plane2.<span style="color: #006600;">doPlane</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>;
   addChild <span style="color: #66cc66;">&#40;</span>plane2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
   plane3=planeFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #66cc66;">&#41;</span>;
   place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   plane3.<span style="color: #006600;">doPlane</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>;
   addChild <span style="color: #66cc66;">&#40;</span>plane3<span style="color: #66cc66;">&#41;</span>;
   plane3.<span style="color: #006600;">rotation</span>=<span style="color: #cc66cc;">0</span>;
&nbsp;
   plane4=planeFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #66cc66;">&#41;</span>;
   place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   plane4.<span style="color: #006600;">doPlane</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>;
   addChild <span style="color: #66cc66;">&#40;</span>plane4<span style="color: #66cc66;">&#41;</span>;
   plane4.<span style="color: #006600;">rotation</span>=<span style="color: #cc66cc;">25</span>;
&nbsp;
   plane5=planeFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;green&quot;</span><span style="color: #66cc66;">&#41;</span>;
   place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   plane5.<span style="color: #006600;">doPlane</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>;
   addChild <span style="color: #66cc66;">&#40;</span>plane5<span style="color: #66cc66;">&#41;</span>;
   plane5.<span style="color: #006600;">rotation</span>=<span style="color: #cc66cc;">180</span>;
&nbsp;
   plane6=planeFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;green&quot;</span><span style="color: #66cc66;">&#41;</span>;
   place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   plane6.<span style="color: #006600;">doPlane</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
   addChild <span style="color: #66cc66;">&#40;</span>plane6<span style="color: #66cc66;">&#41;</span>;
   plane6.<span style="color: #006600;">rotation</span>=<span style="color: #cc66cc;">345</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #808080; font-style: italic;">//Generate extrinsic states</span>
   xPos=<span style="color: #cc66cc;">250</span>;
   yPos +=<span style="color: #cc66cc;">60</span>;
   w=<span style="color: #cc66cc;">10</span>;
   h=<span style="color: #cc66cc;">50</span>;
   curve=<span style="color: #cc66cc;">20</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The Client supplies the values for the extrinsic parameters, but it can also add values for properties of the entire object beyond the extrinsic. An important element is to point out the different rotation values for the object pairs:
</p>
<ul>
<li> red: plane 1-rotation= 15 ; plane2-rotation=none;</li>
<li>blue: plane3 -rotation= 0 ; plane4-rotation= 25;</li>
<li>green: plane5 -rotation= 180 ; plane6-rotation= 345;</li>
</ul>
<p>When the overall rotation is viewed in the output, it’s clear that both images take the rotation value of the last airplane in the script for their group where a rotation value is specified. This can be seen in Figure 3.</p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2007/11/planes.gif"><img src="http://www.as3dp.com/wp-content/uploads/2007/11/planes.gif" /><br />
</a><strong>Figure 3. Three objects with six airplanes</strong></p>
<p>The red airplanes (plane1 and plane2 in the code) at the top of Figure 2 are at a 15° angle (0° is straight up). Because plane1 and plane2 are part of the same “red” instance, their angle is the same. Likewise with the other two pairs, even where one is assigned one rotation value and the other a different one, both images show the same rotation. The <code>x</code> and <code>y</code> values of the airplanes are relative to the instance in which each pair resides. The green airplanes have different noses because they have different values for the curve of the rounded rectangle the makes up the fuselage—made possible because each has an extrinsic value for the curve value of a rounded rectangle. So, while references to the same instance have the same intrinsic value (color in this case), the extrinsic values distinguish one from the other even though they are part of the same flyweight instance. Because rotation is a property of the <code>DisplayObject</code> class, it treats all instances as separate entities, but not the multiple references to the same instance. So when an instance is rotated, all references to that instance are rotated in the same direction.</p>
<p><strong>Multiple Updates</strong></p>
<p>Now that there’s some kind of grasp on what’s going on with flyweights and how they are structured, it’s time to really put them to work. Going back to the original purpose of this Flyweight to place many images on the screen quickly using as little memory as possible and update them regularly, this next application constantly refreshes 50 different images. Each reference to a single instance is stored in an array to quickly add images to the screen in a test of concept. The application employs the following code:</p>
<p><strong>Abstract Flyweight</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code32'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3932"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p39code32"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Abstract class Flyweight</span>
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyweightPlane <span style="color: #0066CC;">extends</span> Sprite
 <span style="color: #66cc66;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">function</span> doPlane <span style="color: #66cc66;">&#40;</span>xPos:uint,yPos:uint,w:uint,h:uint,curve:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
 <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Flyweight Factory</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code33'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3933"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code" id="p39code33"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Flyweight Factory</span>
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PlaneFactory
 <span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">//Associative Array</span>
  protected <span style="color: #000000; font-weight: bold;">var</span> planeIntrinsic:<span style="color: #0066CC;">Object</span>=<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PlaneFactory <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   getPlane <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getPlane <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;east&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> MakePlane<span style="color: #66cc66;">&#40;</span>0x990000<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getIntrinsic <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span>,... <span style="color: #006600;">rest</span><span style="color: #66cc66;">&#41;</span>:FlyweightPlane
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">true</span> :
     <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">false</span> :
     planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>=rest<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
     <span style="color: #b1b100;">break</span>;
   <span style="color: #66cc66;">&#125;</span>
   <span style="color: #b1b100;">return</span> planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Concrete Flyweight</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code34'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3934"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p39code34"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Concrete Flyweight</span>
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakePlane <span style="color: #0066CC;">extends</span> FlyweightPlane
 <span style="color: #66cc66;">&#123;</span>
  protected <span style="color: #000000; font-weight: bold;">var</span> fill: uint;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MakePlane <span style="color: #66cc66;">&#40;</span>fill:uint<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #0066CC;">this</span>.<span style="color: #006600;">fill</span>=fill;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
override <span style="color: #000000; font-weight: bold;">function</span> doPlane <span style="color: #66cc66;">&#40;</span>xPos:uint,yPos:uint,w:uint,h:uint,curve:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   graphics.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>
   graphics.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span>xPos-<span style="color: #cc66cc;">18</span>,yPos+<span style="color: #cc66cc;">17</span>,<span style="color: #cc66cc;">45</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span>
   graphics.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span>xPos-<span style="color: #cc66cc;">7</span>,yPos+<span style="color: #cc66cc;">37</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>
   graphics.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Client</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code35'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3935"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
</pre></td><td class="code" id="p39code35"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Client class</span>
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #0066CC;">setInterval</span>;
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">Timer</span>;
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">TimerEvent</span>;
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Apprentice <span style="color: #0066CC;">extends</span> Sprite
 <span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">//Client data to provide extrinsic state details</span>
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> xPos:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> yPos:uint=<span style="color: #cc66cc;">50</span>;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> w:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> h:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> heading:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> curve:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> plane:<span style="color: #0066CC;">Array</span>;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sweepTime:uint=<span style="color: #cc66cc;">1000</span>;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ee:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span>;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> planeFactory:PlaneFactory;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sweep:Timer;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Apprentice <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   planeFactory=<span style="color: #000000; font-weight: bold;">new</span> PlaneFactory ;
   plane=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
   upDate <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   sweep=<span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span>sweepTime,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
   sweep.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;timer&quot;</span>,newPos<span style="color: #66cc66;">&#41;</span>
   sweep.<span style="color: #0066CC;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeGroup <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   yPos=<span style="color: #cc66cc;">80</span>;
   heading=<span style="color: #cc66cc;">90</span>;
   <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> air:uint=<span style="color: #cc66cc;">0</span>; air <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">50</span>; air++<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
    plane<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>=planeFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;east&quot;</span><span style="color: #66cc66;">&#41;</span>;
    place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    plane<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">doPlane</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>;
    addChild <span style="color: #66cc66;">&#40;</span>plane<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
    plane<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">rotation</span>=heading;
   <span style="color: #66cc66;">&#125;</span>
   plane<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">700</span>,plane<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">150</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #808080; font-style: italic;">//Generate extrinsic states</span>
   xPos=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span>;
   yPos= <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">400</span><span style="color: #66cc66;">&#41;</span>;
   w=<span style="color: #cc66cc;">10</span>;
   h=<span style="color: #cc66cc;">50</span>;
   curve=<span style="color: #cc66cc;">20</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> cleanup <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> air:uint=<span style="color: #cc66cc;">0</span>; air <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">50</span>; air++<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
   removeChild <span style="color: #66cc66;">&#40;</span>plane<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
   plane<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">null</span>;
   <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newPos <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   upDate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> upDate <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>ee<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
    cleanup <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #66cc66;">&#125;</span>
   makeGroup <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This flyweight application is very similar to the others except that it uses a timer to refresh the images and an array to hold the different instance references. The idea is to simulate the changing positions of aircraft, but instead of changing the actual <code>x</code> and <code>y</code> positions as the aircraft enter the traffic pattern prior to landing, it uses random positioning values to demonstrating quickly rendering large number of images to the screen in different positions.</p>
<p><strong>The Sorcerer’s Apprentice</strong></p>
<p>The application refreshes the images every second, but what occurs is unexpected. Figures 4-6 show the rapid increase of images on the screen:</p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2007/11/first.gif"><img src="http://www.as3dp.com/wp-content/uploads/2007/11/first.gif" /><br />
</a><strong>Figure 4:  The expected number of images appears using a single instance.</strong></p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2007/11/multiple.gif"><img src="http://www.as3dp.com/wp-content/uploads/2007/11/multiple.gif" /><br />
</a><strong>Figure 5: After a few seconds, it becomes apparent that the associative array is loading the new references on top of the old ones.</strong></p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2007/11/flood.gif"><img src="http://www.as3dp.com/wp-content/uploads/2007/11/flood.gif" /><br />
</a><strong>Figure 5: Eventually the images become unrecognizable as more and more images are placed</strong></p>
<p>The results of this application reminded me of Mickey Mouse playing the sorcerer’s apprentice in Disney’s animated classic, <em>Fantasia</em>. Based on a poem by the same name by Johann Wolfgang von Goethe, the following lines showed that Goethe knew we’d be working with sprites sooner or later: </p>
<blockquote><p><em>Every step and saying<br />
That he used, I know,<br />
And with sprites obeying<br />
My arts I will show.</em> </p></blockquote>
<p>In the animated 1940 film Mickey Mouse decides to use the sorcerer’s magic to do his chores—carrying water from the well up to a tank to be filled. He uses magic on some brooms and they begin carrying the water up to the tank, but soon the tank fills and he doesn’t know the magic words to stop them, and they just keep on going very much like this application.</p>
<p><strong>The Wrong Spell</strong></p>
<p>The method used to refresh the sprites removed both the image from the stage and set the elements to null.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code36'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3936"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p39code36"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> cleanup <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> air:uint=<span style="color: #cc66cc;">0</span>; air <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">50</span>; air++<span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">&#123;</span>
 removeChild <span style="color: #66cc66;">&#40;</span>plane<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
 plane<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">null</span>;
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>However, the results show that the <code>cleaup() </code>method simply does not work. So what’s going on? How is it possible for the array to keep adding to the number of images even though the array and all of the elements are cleared from the stage and set to null? The answer is that the method addresses the wrong array. This shows that the <code>Array</code> class used to generate references to the instance of the Flyweight object did not store those references. Rather, they are stored in the associative array in the <code>Flyweight Factory</code>. Therefore, we need a method in the <code>Flyweight Factory</code> to clear them out with every update. Fortunately, it’s quite simple. All that is required to set a given key in the associative array to null. So, in the above example, the following line will do the trick.</p>
<p><code>planeIntrinsic["east"]=null;</code></p>
<p>Because a single instance was generating several references with position information and other extrinsic values, every time the set of 50 images was refreshed, it just added to those stored in the associative array. The Array class worked to generate new references to the associative array, but it never removed any. All that the cleanup script did was to empty the contents of the array that shoved more references into the instance held in the associative array.</p>
<p><strong>Multiple Instances and Many References</strong></p>
<p>The final step is to install a system that deals with multiple instances, with each instance referenced by an updated set of new extrinsic values. To continue with the set used so far, this final application will take four instances with each on a different heading and updated using random values. The updates will be every 3 seconds (or 3000 milliseconds) and each set will display a set of 200 images based on four instances, each represented by a different color. The following script finally pulls all of the parts together:</p>
<p><strong>Flyweight</strong>
</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code37'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3937"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p39code37"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Abstract class Flyweight</span>
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyweightPlane <span style="color: #0066CC;">extends</span> Sprite
 <span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">function</span> doPlane <span style="color: #66cc66;">&#40;</span>xPos:uint,yPos:uint,w:uint,h:uint,curve:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
   <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Flyweight Factory</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code38'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3938"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
</pre></td><td class="code" id="p39code38"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Flyweight Factory</span>
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PlaneFactory
 <span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">//Associative Array</span>
  protected <span style="color: #000000; font-weight: bold;">var</span> planeIntrinsic:<span style="color: #0066CC;">Object</span>=<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PlaneFactory <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   getPlane <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getPlane <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;east&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> MakePlane<span style="color: #66cc66;">&#40;</span>0x990000<span style="color: #66cc66;">&#41;</span>;
   planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;west&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> MakePlane<span style="color: #66cc66;">&#40;</span>0x009900<span style="color: #66cc66;">&#41;</span>;
   planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;north&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> MakePlane<span style="color: #66cc66;">&#40;</span>0x000099<span style="color: #66cc66;">&#41;</span>;
   planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;south&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> MakePlane<span style="color: #66cc66;">&#40;</span>0xFF6600<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getIntrinsic <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span>,... <span style="color: #006600;">rest</span><span style="color: #66cc66;">&#41;</span>:FlyweightPlane
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">true</span> :
     <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">false</span> :
     planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>=rest<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
     <span style="color: #b1b100;">break</span>;
   <span style="color: #66cc66;">&#125;</span>
   <span style="color: #b1b100;">return</span> planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clearPlane <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;east&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">null</span>;
   planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;west&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">null</span>;
   planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;north&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">null</span>;
   planeIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;south&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">null</span>;
   getPlane <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Concrete Flyweight</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code39'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3939"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p39code39"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Concrete Flyweight</span>
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakePlane <span style="color: #0066CC;">extends</span> FlyweightPlane
 <span style="color: #66cc66;">&#123;</span>
  protected <span style="color: #000000; font-weight: bold;">var</span> fill: uint;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MakePlane <span style="color: #66cc66;">&#40;</span>fill:uint<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #0066CC;">this</span>.<span style="color: #006600;">fill</span>=fill;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
override <span style="color: #000000; font-weight: bold;">function</span> doPlane <span style="color: #66cc66;">&#40;</span>xPos:uint,yPos:uint,w:uint,h:uint,curve:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   graphics.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>
   graphics.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span>xPos-<span style="color: #cc66cc;">18</span>,yPos+<span style="color: #cc66cc;">17</span>,<span style="color: #cc66cc;">45</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span>
   graphics.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
   graphics.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span>xPos-<span style="color: #cc66cc;">7</span>,yPos+<span style="color: #cc66cc;">37</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>
   graphics.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Client</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code40'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3940"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
</pre></td><td class="code" id="p39code40"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">//Client class</span>
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #0066CC;">setInterval</span>;
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">Timer</span>;
 <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">TimerEvent</span>;
&nbsp;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CrowdedSky <span style="color: #0066CC;">extends</span> Sprite
 <span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">//Client data to provide extrinsic state details</span>
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> xPos:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> yPos:uint=<span style="color: #cc66cc;">50</span>;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> w:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> h:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> heading:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> curve:uint;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> planeE:<span style="color: #0066CC;">Array</span>;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> planeW:<span style="color: #0066CC;">Array</span>;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> planeN:<span style="color: #0066CC;">Array</span>;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> planeS:<span style="color: #0066CC;">Array</span>;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sweepTime:uint=<span style="color: #cc66cc;">3000</span>;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ee:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span>;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> planeFactory:PlaneFactory;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sweep:Timer;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> CrowdedSky <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   planeFactory=<span style="color: #000000; font-weight: bold;">new</span> PlaneFactory ;
   planeE=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   planeW=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   planeN=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   planeS=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
   upDate <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   sweep=<span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span>sweepTime,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
   sweep.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;timer&quot;</span>,newPos<span style="color: #66cc66;">&#41;</span>
   sweep.<span style="color: #0066CC;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeEast <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   yPos=<span style="color: #cc66cc;">80</span>;
   heading=<span style="color: #cc66cc;">315</span>;
   <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> air:uint=<span style="color: #cc66cc;">0</span>; air <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">50</span>; air++<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
    planeE<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>=planeFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;east&quot;</span><span style="color: #66cc66;">&#41;</span>;
    place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    planeE<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">doPlane</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>;
    addChild <span style="color: #66cc66;">&#40;</span>planeE<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
    planeE<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">rotation</span>=heading;
   <span style="color: #66cc66;">&#125;</span>
   ee=<span style="color: #000000; font-weight: bold;">true</span>;
   planeE<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">350</span>,planeE<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">700</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeWest <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   yPos=<span style="color: #cc66cc;">80</span>;
   heading=<span style="color: #cc66cc;">225</span>;
   <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> air:uint=<span style="color: #cc66cc;">0</span>; air <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">50</span>; air++<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
    planeW<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>=planeFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;west&quot;</span><span style="color: #66cc66;">&#41;</span>;
    place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    planeW<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">doPlane</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>;
    addChild <span style="color: #66cc66;">&#40;</span>planeW<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
    planeW<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">rotation</span>=heading;
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   planeW<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">700</span>,planeW<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">500</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeNorth <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   yPos=<span style="color: #cc66cc;">80</span>;
   heading=<span style="color: #cc66cc;">45</span>;
   <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> air:uint=<span style="color: #cc66cc;">0</span>; air <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">50</span>; air++<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
    planeN<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>=planeFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;north&quot;</span><span style="color: #66cc66;">&#41;</span>;
    place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    planeN<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">doPlane</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>;
    addChild <span style="color: #66cc66;">&#40;</span>planeN<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
    planeN<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">rotation</span>=heading;
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   planeN<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">250</span>,planeN<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">200</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeSouth <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   yPos=<span style="color: #cc66cc;">0</span>;
   heading=<span style="color: #cc66cc;">135</span>;
   <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> air:uint=<span style="color: #cc66cc;">0</span>; air <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">50</span>; air++<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
    planeS<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>=planeFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;south&quot;</span><span style="color: #66cc66;">&#41;</span>;
    place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    planeS<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">doPlane</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,w,h,curve<span style="color: #66cc66;">&#41;</span>;
    addChild <span style="color: #66cc66;">&#40;</span>planeS<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
    planeS<span style="color: #66cc66;">&#91;</span>air<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">rotation</span>=heading;
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   planeS<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">650</span>,planeS<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">150</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> place <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #808080; font-style: italic;">//Generate extrinsic states</span>
   xPos=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span>;
   yPos= <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">400</span><span style="color: #66cc66;">&#41;</span>;
   w=<span style="color: #cc66cc;">10</span>;
   h=<span style="color: #cc66cc;">50</span>;
   curve=<span style="color: #cc66cc;">20</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> cleanup <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   removeChild <span style="color: #66cc66;">&#40;</span>planeE<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
   removeChild <span style="color: #66cc66;">&#40;</span>planeW<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
   removeChild <span style="color: #66cc66;">&#40;</span>planeN<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
   removeChild <span style="color: #66cc66;">&#40;</span>planeS<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
   planeE<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>=planeFactory.<span style="color: #006600;">clearPlane</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newPos <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   upDate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> upDate <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>ee<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
    cleanup <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #66cc66;">&#125;</span>
   makeEast <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   makeWest <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   makeNorth <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   makeSouth <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Figure 7 shows the final results. The underlying radar sweep is simply a movie clip running beneath the display.</p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2007/11/complete.gif" title="Figure 7: Flyweight generating 200 images." rel="lightbox"><img src="http://www.as3dp.com/wp-content/uploads/2007/11/complete.gif" height="353" width="518" /></a><strong><br />
Figure 7: Flyweight generating 200 images.</strong><strong>Flyweight Design Pattern Reconsidered</strong></p>
<p>In working through this pattern, which can be exhausting, it has a number of characteristics that are still elusive. The role of the unshared concrete flyweight is not clear, and some the examples I’ve seen may have missed the mark altogether. The use of the unshared concrete flyweight for assigning values for extrinsic states and how these are used in concert with a client with references to a shared object is not clear in most examples of the flyweight. However, it is a great pattern for understanding class relations and the nature of objects.</p>
<p>All in all, the Flyweight design pattern holds valuable lessons about OOP, especially some of the subtler features and certainly participant relations.With the speed of processors developing at high rates and storage now measured in gigabytes (or terabytes), whether the speed increase gained is worth the added effort is not as obvious as it once was. However, if the asymptotic performance is optimal, I see nothing wrong with looking for ways to speed up the display of large numbers of elements on a stage. I doubt that the 200 images (or even 1000) would be enough to warrant spending too much time on the Flyweight, but when the numbers being reaching 10,000 or even higher, an extra byte or two might come in handy. </p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2007%2F11%2Fthe-flyweight-design-pattern-where-shared-objects-solve-storage-problems%2F&amp;title=The%20Flyweight%20Design%20Pattern%3A%20Where%20Shared%20Objects%20Solve%20Storage%20Problems" id="wpa2a_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2007/11/the-flyweight-design-pattern-where-shared-objects-solve-storage-problems/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>The ActionScript 3.0 Flyweight Saga: Part III Aggregation Aggravation, Stuff on the Stage and the Intrinsic State</title>
		<link>http://www.as3dp.com/2007/09/the-actionscript-30-flyweight-saga-part-iii-aggregation-aggravation-stuff-on-the-stage-and-the-intrinsic-state/</link>
		<comments>http://www.as3dp.com/2007/09/the-actionscript-30-flyweight-saga-part-iii-aggregation-aggravation-stuff-on-the-stage-and-the-intrinsic-state/#comments</comments>
		<pubDate>Tue, 18 Sep 2007 13:21:37 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Flyweight]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2007/09/18/the-actionscript-30-flyweight-saga-part-iii-aggregation-aggravation-stuff-on-the-stage-and-the-intrinsic-state/</guid>
		<description><![CDATA[Aggregation Aggravation In the first installment of this Flyweight Saga, I noted that the relationship between a Flyweight and Flyweight Factory class is one of aggregation. The initial example shows that the Retrieve method in the Factory class returns an instance of IFlyweight, meeting the requirement of the proper connection between the two classes. In [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Aggregation Aggravation</strong></p>
<p>In the first installment of this <em>Flyweight Saga</em>, I noted that the relationship between a Flyweight and Flyweight Factory class is one of <strong>aggregation</strong>. The initial example shows that the <code>Retrieve</code> method in the <code>Factory</code> class returns an instance of <code>IFlyweight</code>, meeting the requirement of the proper connection between the two classes.  In looking at one example in Java, (even with modest Java skills), the program clearly did not have such a relationship between the Factory and Flyweight. In fact, it claimed, that an object’s extrinsic state can be shared by classes. Now, maybe that was unfortunate wording because the big feature of the Flyweight design pattern is that the Flyweight can be a shared object, but only the intrinsic state can be shared. (Maybe the author meant that extrinsic states can be shared between classes in a Flyweight design pattern, and that’s probably right but is not a key feature of the pattern.)<br />
<span id="more-35"></span><br />
Anyway, after looking at several different descriptions of aggregation, including the one provided by the GoF, it’s clear that the concept is one with fuzzy borders and can slip into either general <strong>composition</strong> or <strong>acquaintance</strong>. It implies that the Flyweight Factory aggregates the Flyweight—no Flyweight, no Factory. As a result, the life of the aggregator (Flyweight Factory) depends on the life of the aggregatee (Flyweight). Like acquaintance, aggregation is implemented with references or pointers rather than defining variables of once class in another. (Apparently C++ is an exception and does set up aggregation by defining variables from the aggregatee class.)</p>
<p>By and large the issue has not been especially significant so far because all of the output was using trace statements, and so output was largely confined to built-in features that only work when the code is run in test mode. It’s great for debugging up to a point, but developing with trace statements that do not take into account how certain graphic elements, especially those that are accessed by extending the Sprite classes, can generate unusable structures for applications that employ graphics and other elements that require the import and extension of other classes. In this next Flyweight example, we leave the realm of trace and use the graphics property (from the Sprite class) to draw solid balls using fill methods. The ball class extends Sprite and implements the interface. That’s all fine and good, but the aggregation becomes problematic in even the simplest example. In taking the general structure from the examples examined up to this point (Parts I and II of the Flyweight Saga), we can begin to see the trouble.</p>
<p>Because of the need to introduce the <code>Sprite</code> reference somewhere in the Flyweight by extending the Sprite class the Flyweight had to be referenced indirectly through the Concrete Flyweight (FlyBall). Does this count? I don’t know, but using an interface instead of an abstract class, I could see no way in getting in the needed Sprite extension. However, I do think that the Flyweight Factory did accomplish the manufacture of a single instance with multiple copies of an object with both the red and yellow balls. The following code is the first go at this using something other than trace. However, is it indeed a true Flyweight design pattern?</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p35code43'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3543"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
</pre></td><td class="code" id="p35code43"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Flyweight</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IFlyweightBall
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> doCircle <span style="color: #66cc66;">&#40;</span>xPos:uint,yPos:uint,radius:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Flyweight Factory</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BallFactory
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> ballIntrinsic:<span style="color: #0066CC;">Object</span>=<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BallFactory <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> FlyBall<span style="color: #66cc66;">&#40;</span>0x990000<span style="color: #66cc66;">&#41;</span>;
			ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;yellow&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> FlyBall<span style="color: #66cc66;">&#40;</span>0xffff00<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getIntrinsic <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span>,... <span style="color: #006600;">rest</span><span style="color: #66cc66;">&#41;</span>:FlyBall
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">true</span> :
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">false</span> :
					ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>=rest<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
					<span style="color: #b1b100;">break</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Flyweight</span>
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyBall <span style="color: #0066CC;">extends</span> Sprite <span style="color: #0066CC;">implements</span> IFlyweightBall
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> fill: uint;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FlyBall <span style="color: #66cc66;">&#40;</span>fill:uint<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">fill</span>=fill;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doCircle <span style="color: #66cc66;">&#40;</span>xPos:uint,yPos:uint,radius:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			graphics.<span style="color: #0066CC;">beginFill</span> <span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
			graphics.<span style="color: #006600;">drawCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
			graphics.<span style="color: #0066CC;">endFill</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Client class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BallClient <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//Client data to provide extrinsic state details</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> xPos;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> yPos;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> radius;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BallClient <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> ballFactory:BallFactory=<span style="color: #000000; font-weight: bold;">new</span> BallFactory;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> cir1:FlyBall=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#41;</span>;
			shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cir1.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>cir1<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> cir2:FlyBall=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#41;</span>;
			shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cir2.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>cir2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> cir3:FlyBall=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yellow&quot;</span><span style="color: #66cc66;">&#41;</span>;
			shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cir3.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>cir3<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> cir4:FlyBall=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yellow&quot;</span><span style="color: #66cc66;">&#41;</span>;
			shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cir4.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>cir4<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Constructed in client</span>
			<span style="color: #000000; font-weight: bold;">var</span> cir5:FlyBall=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;green&quot;</span>,<span style="color: #000000; font-weight: bold;">new</span> FlyBall<span style="color: #66cc66;">&#40;</span>0x009900<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cir5.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>cir5<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Generate extrinsic states</span>
			xPos=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span>;
			yPos=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span>;
			radius=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>When this application is tested, you will see two red balls, two yellow balls and a green ball seen in Figure 1. The following line in the Flyweight Factory,</p>
<p><code>public function getIntrinsic (key:String,... rest):FlyBall</code></p>
<p>sets up the returned data as a concrete flyweight (FlyBall). In looking at the UML diagram for the Flyweight (See Part I of the Flyweight saga), the Flyweight Factory is an aggregation of the Flyweight. The aggregation relationship is one where the aggregator (Flyweight Factory) holds a reference to the aggregatee (Flyweight), but as you can see, the reference is to the Concrete Flyweight.</p>
<p><a href='http://www.as3dp.com/wp-content/uploads/2007/09/flyballs.gif' title='Figure 1'><img src='http://www.as3dp.com/wp-content/uploads/2007/09/flyballs.gif' alt='Figure 1' /></a></p>
<p><strong>Abstract Class Changes Everything</strong></p>
<p>The first ActionScript 3.0 Flyweight I worked was the one Jim Kremens had made for test of concept. It employed an <strong>abstract class</strong> rather than an <strong>interface</strong>, and I changed it to an interface. Now, I’ve come full circle back to the abstract class because it can be set up to inherit the needed elements of the <code>Sprite</code> class. This allows the Factory to establish a reference to the Flyweight rather than having to use a concrete Flyweight. The resulting application now has all of the right relationships.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p35code44'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3544"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
</pre></td><td class="code" id="p35code44"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Abstract class Flyweight</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyweightBall <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">function</span> doCircle <span style="color: #66cc66;">&#40;</span>xPos:uint,yPos:uint,radius:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
			<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Flyweight Factory</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BallFactory
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> ballIntrinsic:<span style="color: #0066CC;">Object</span>=<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BallFactory <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> FlyBall<span style="color: #66cc66;">&#40;</span>0x990000<span style="color: #66cc66;">&#41;</span>;
			ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;yellow&quot;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #000000; font-weight: bold;">new</span> FlyBall<span style="color: #66cc66;">&#40;</span>0xffff00<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getIntrinsic <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span>,... <span style="color: #006600;">rest</span><span style="color: #66cc66;">&#41;</span>:FlyweightBall
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">true</span> :
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">false</span> :
					ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>=rest<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
					<span style="color: #b1b100;">break</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> ballIntrinsic<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Flyweight</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyBall <span style="color: #0066CC;">extends</span> FlyweightBall
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> fill: uint;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FlyBall <span style="color: #66cc66;">&#40;</span>fill:uint<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">fill</span>=fill;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #000000; font-weight: bold;">function</span> doCircle <span style="color: #66cc66;">&#40;</span>xPos:uint,yPos:uint,radius:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			graphics.<span style="color: #0066CC;">beginFill</span> <span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
			graphics.<span style="color: #006600;">drawCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
			graphics.<span style="color: #0066CC;">endFill</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Client class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BallClient <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//Client data to provide extrinsic state details</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> xPos;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> yPos;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> radius;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BallClient <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> ballFactory:BallFactory=<span style="color: #000000; font-weight: bold;">new</span> BallFactory;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> cir1:FlyweightBall=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#41;</span>;
			shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cir1.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>cir1<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> cir2:FlyweightBall=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#41;</span>;
			shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cir2.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>cir2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> cir3:FlyweightBall=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yellow&quot;</span><span style="color: #66cc66;">&#41;</span>;
			shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cir3.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>cir3<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> cir4:FlyweightBall=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yellow&quot;</span><span style="color: #66cc66;">&#41;</span>;
			shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cir4.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>cir4<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Constructed in client</span>
			<span style="color: #000000; font-weight: bold;">var</span> cir5:FlyweightBall=ballFactory.<span style="color: #006600;">getIntrinsic</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;green&quot;</span>,<span style="color: #000000; font-weight: bold;">new</span> FlyBall<span style="color: #66cc66;">&#40;</span>0x009900<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cir5.<span style="color: #006600;">doCircle</span> <span style="color: #66cc66;">&#40;</span>xPos,yPos,radius<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>cir5<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Generate extrinsic states</span>
			xPos=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span>;
			yPos=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span>;
			radius=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>The Intrinsic State</strong></p>
<p><em>Part II of the Flyweight Saga</em> examined  <strong>extrinsic</strong> states, which are part of the key operation in the Flyweight. However, it is the <strong>intrinsic</strong> state in the concrete flyweight where we need to look now. In looking at the UML diagram, the intrinsic state is stored in the Concrete Flyweight. So, while extending the Flyweight and implementing the key operation, we need to add an intrinsic state. In this case, the intrinsic case is placed as a parameter within the concrete flyweight constructor. The following lines are the crux of the intrinsic state:</p>
<p><code>protected var fill: uint;<br />
public function FlyBall (fill:uint)<br />
{<br />
      this.fill=fill;<br />
}</code></p>
<p>The protected variable <code>fill</code> is the key here. As part of the constructor, the <code>fill</code> value is stored in the concrete flyweight. Then, when the operation calls the extrinsic parameters, the <code>fill </code>variable provides the color code for the concrete flyweight’s implementation of the instance. So, once the ball instance is created in the factory participant (<code>BallFactory</code>), the intrinsic value remains constant no matter what the values of the extrinsic values passed in the concrete flyweight method. As a result, it is possible to make multiple instances of the red and yellow balls using the intrinsic color that was in the associative array (<code>ballIntrinsic</code>). Thus, the line,</p>
<p><code>ballIntrinsic["red"]=new FlyBall(0x990000);</code></p>
<p>creates a  “red” instance providing the fill color value in the construction parameter (<code>0x990000</code>). If the same key is called from the client, the <code>getIntrinsic</code> function checks to see if it exists or not. If it does, it simply returns the instance with the included color value stored as an intrinsic state. The extrinsic states, which include the radius, and x and y positions, simply re-use the existing red ball and change only the extrinsic states. (See <em>Flyweight Saga Part II</em>.)</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2007%2F09%2Fthe-actionscript-30-flyweight-saga-part-iii-aggregation-aggravation-stuff-on-the-stage-and-the-intrinsic-state%2F&amp;title=The%20ActionScript%203.0%20Flyweight%20Saga%3A%20Part%20III%20Aggregation%20Aggravation%2C%20Stuff%20on%20the%20Stage%20and%20the%20Intrinsic%20State" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2007/09/the-actionscript-30-flyweight-saga-part-iii-aggregation-aggravation-stuff-on-the-stage-and-the-intrinsic-state/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>The ActionScript 3.0 Flyweight Saga: Part II Extrinsic States</title>
		<link>http://www.as3dp.com/2007/07/the-actionscript-30-flyweight-saga-part-ii-extrinsic-states/</link>
		<comments>http://www.as3dp.com/2007/07/the-actionscript-30-flyweight-saga-part-ii-extrinsic-states/#comments</comments>
		<pubDate>Mon, 23 Jul 2007 13:11:55 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Flyweight]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2007/07/23/the-actionscript-30-flyweight-saga-part-ii-extrinsic-states/</guid>
		<description><![CDATA[Extrinsic States In the first part of the Flyweight Saga, I had the idea that a Flyweight design pattern would be a good idea because it would be useful for cranking out buttons on the stage. However, the comments by the readers have led me to reconsider that idea; so it’s back to the drawing [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Extrinsic States</strong><br />
In the first part of the Flyweight Saga, I had the idea that a Flyweight design pattern would be a good idea because it would be useful for cranking out buttons on the stage. However, the comments by the readers have led me to reconsider that idea; so it’s back to the drawing board. This revised Flyweight is going to focus on adding an extrinsic state parameter. Also, I got rid of all but one of the concrete Flyweights and am now down to the FlyButton class only in order to focus on extrinsic states.</p>
<p>Keep in mind that this process is a matter of working out the specific sense of a Flyweight pattern more than it is to show the optimum example. In going over some Flyweight materials from MIT, they suggested having a grasp on other, <em>simpler</em> patterns like the Observer and even MVC before tackling the Flyweight. This was both comforting and worrisome!</p>
<p><strong>Extrinsic State Parameter</strong></p>
<p>To get back on track (somewhat), this new Flyweight includes a parameter for <em>extrinsic states</em> in the <code>Flyweight</code> interface. Because the state is extrinsic, it changes with the Flyweights context. As discussed briefly in Part I of this saga, the <em>extrinsic</em> state changes with the context while the <em>intrinsic </em>state does not. Thus, the extrinsic state is <em>not shareable</em> (a shared object) and the intrinsic is. A concrete class can be sharable or not, but it cannot be if it does not store the intrinsic state. However, both sharable and non-sharable concrete Flyweights can exist. The following summary might be helpful.<br />
<strong>Extrinsic state</strong></p>
<ul>
Cannot be shared<br />
Depends on flyweight’s context<br />
Client is responsible for supplying extrinsic state when needed</ul>
<p><strong>Intrinsic state</strong></p>
<ul>
Can be shared<br />
Stored (inherent) in the flyweight<br />
Does not depend flyweight’s context</ul>
<p><span id="more-30"></span><br />
<strong>Adding a Numeric State Changer</strong><br />
To get started this first new Flyweight does nothing more than add an extrinsic parameter to the existing Flyweight and adds a few new wrinkles. The Flyweight interface key operation includes a numeric (<code>uint</code>) parameter used as a state change indicator. The intrinsic characteristic of the concrete Flyweight, FlyButton, is nothing more than the part of the trace statement indicating “Fly button #.” The extrinsic value (state) is  the extrinsic variable that is incremented with each use.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p30code49'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3049"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
</pre></td><td class="code" id="p30code49"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Interface</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IFlyweight
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> build <span style="color: #66cc66;">&#40;</span>extrinsic:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">////////</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Factory</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Factory
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> objects:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Factory <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			objects<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;nav&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> FlyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			objects<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;send&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> FlyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			objects<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;about&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> FlyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Retrieve <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span>, ...<span style="color: #006600;">rest</span><span style="color: #66cc66;">&#41;</span>:IFlyweight
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>objects<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> :
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> :
					objects<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> = rest<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
					<span style="color: #b1b100;">break</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> objects<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">////////</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Flyweight (shared)</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyButton <span style="color: #0066CC;">implements</span> IFlyweight
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FlyButton <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> build <span style="color: #66cc66;">&#40;</span>extrinsic:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Fly button #&quot;</span> + extrinsic <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">////////</span>
&nbsp;
package  <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Client class</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ButtonMaker <span style="color: #0066CC;">extends</span> Sprite <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> extrinsic:uint;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ButtonMaker<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
			extrinsic=<span style="color: #cc66cc;">1</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> factory:Factory  = <span style="color: #000000; font-weight: bold;">new</span> Factory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> nav:IFlyweight = factory.<span style="color: #006600;">Retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;nav&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//&quot;Nav&quot; created in Factory</span>
			nav.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span>extrinsic++<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> launch:IFlyweight = factory.<span style="color: #006600;">Retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;send&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//&quot;Send&quot; created in Factory</span>
			launch.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span>extrinsic++<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> about:IFlyweight = factory.<span style="color: #006600;">Retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;about&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//&quot;About &quot; created in Factory</span>
			about.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span>extrinsic++<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> playit:IFlyweight = factory.<span style="color: #006600;">Retrieve</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;play&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> FlyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			playit.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span>extrinsic++<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>That seems to work fine, and most of the buttons were generated in the Factory, but one is created in the client. Given the nature of the design, that’s fine. You will see the following output:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p30code50'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3050"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p30code50"><pre class="actionscript" style="font-family:monospace;">Fly <span style="color: #0066CC;">button</span> <span style="color: #808080; font-style: italic;">#1</span>
Fly <span style="color: #0066CC;">button</span> <span style="color: #808080; font-style: italic;">#2</span>
Fly <span style="color: #0066CC;">button</span> <span style="color: #808080; font-style: italic;">#3</span>
Fly <span style="color: #0066CC;">button</span> <span style="color: #808080; font-style: italic;">#4</span></pre></td></tr></table></div>

<p><strong>The Adding Something a Button Could Use</strong><br />
Keeping in mind that we’re looking for a better way to generate buttons to be placed on the page, we will need to add parameters to the existing interface. The needs are simple and identical for each button: a name, an x position, and a y position. So, all that’s necessary is to add parameters for those values and then add something to the client that generates those values. This next example gets us closer to that goal.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p30code51'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3051"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
</pre></td><td class="code" id="p30code51"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Parameters for button label and x and y values</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IFlyweight
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> build <span style="color: #66cc66;">&#40;</span>bname:<span style="color: #0066CC;">String</span>,xpos:uint,ypos:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">////////</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Flyweight Factory</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Factory
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> objects:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Factory <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			objects<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;nav&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> FlyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			objects<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;send&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> FlyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			objects<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;about&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> FlyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Retrieve <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span>, ...<span style="color: #006600;">rest</span><span style="color: #66cc66;">&#41;</span>:IFlyweight
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>objects<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> :
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> :
					objects<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> = rest<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
					<span style="color: #b1b100;">break</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> objects<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">////////</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Flyweight</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyButton <span style="color: #0066CC;">implements</span> IFlyweight
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FlyButton <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> build <span style="color: #66cc66;">&#40;</span>bname:<span style="color: #0066CC;">String</span>,xpos:uint,ypos:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Fly button &quot;</span> + bname+ <span style="color: #ff0000;">&quot; is located at &quot;</span> + xpos +<span style="color: #ff0000;">&quot;,&quot;</span>+ypos<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">////////</span>
&nbsp;
package  <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Client class</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NamePlace <span style="color: #0066CC;">extends</span> Sprite <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//Client data to provide extrinsic state details</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bnamesA:<span style="color: #0066CC;">Array</span>=<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Play&quot;</span>,<span style="color: #ff0000;">&quot;About Us&quot;</span>,<span style="color: #ff0000;">&quot;Send&quot;</span>,<span style="color: #ff0000;">&quot;Navigation&quot;</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> xposA:<span style="color: #0066CC;">Array</span>=<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> yposA:<span style="color: #0066CC;">Array</span>=<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">60</span>,<span style="color: #cc66cc;">80</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> NamePlace<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> factory:Factory  = <span style="color: #000000; font-weight: bold;">new</span> Factory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> nav:IFlyweight = factory.<span style="color: #006600;">Retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;nav&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//&quot;Nav&quot; already exists</span>
			nav.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span>bnamesA.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,xposA.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,yposA.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> launch:IFlyweight = factory.<span style="color: #006600;">Retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;send&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//&quot;Send&quot; already exists</span>
			launch.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span>bnamesA.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,xposA.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,yposA.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> about:IFlyweight = factory.<span style="color: #006600;">Retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;about&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//&quot;About &quot; already exists</span>
			about.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span>bnamesA.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,xposA.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,yposA.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> playit:IFlyweight = factory.<span style="color: #006600;">Retrieve</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;play&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> FlyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			playit.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span>bnamesA.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,xposA.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,yposA.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This time the output is going to look a lot more like what we need for the buttons. The name of the button, and it’s x,y location all appear in the output as the following shows:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p30code52'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3052"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p30code52"><pre class="actionscript" style="font-family:monospace;">Fly <span style="color: #0066CC;">button</span> Navigation is located at <span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">80</span>
Fly <span style="color: #0066CC;">button</span> <span style="color: #0066CC;">Send</span> is located at <span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">60</span>
Fly <span style="color: #0066CC;">button</span> About Us is located at <span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">40</span>
Fly <span style="color: #0066CC;">button</span> <span style="color: #0066CC;">Play</span> is located at <span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">20</span></pre></td></tr></table></div>

<p>Going back to the comments made on this blog, it’s probably wise to create all of the new buttons in the Factory. Also, we need to take a closer look at what is going on with intrinsic values in the FlyButton class. However, it’s creeping along, and in doing so gets us closer to the goal.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2007%2F07%2Fthe-actionscript-30-flyweight-saga-part-ii-extrinsic-states%2F&amp;title=The%20ActionScript%203.0%20Flyweight%20Saga%3A%20Part%20II%20Extrinsic%20States" id="wpa2a_6"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2007/07/the-actionscript-30-flyweight-saga-part-ii-extrinsic-states/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The ActionScript 3.0 Flyweight Saga: Part I</title>
		<link>http://www.as3dp.com/2007/06/the-actionscript-30-flyweight-saga-part-i/</link>
		<comments>http://www.as3dp.com/2007/06/the-actionscript-30-flyweight-saga-part-i/#comments</comments>
		<pubDate>Sat, 09 Jun 2007 12:03:11 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Flyweight]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2007/06/09/the-actionscript-30-flyweight-saga-part-i/</guid>
		<description><![CDATA[I ran across the Flyweight design pattern when looking for a pattern that could be used to speed up placing objects on the stage in a Flash application using ActionScript 3.0. The Flyweight name implies a somewhat insignificant design pattern, but after working with it for a few months I found it to be both [...]]]></description>
			<content:encoded><![CDATA[<p>I ran across the Flyweight design pattern when looking for a pattern that could be used to speed up placing objects on the stage in a Flash application using ActionScript 3.0. The Flyweight name implies a somewhat insignificant design pattern, but after working with it for a few months I found it to be both powerful and informative.  It is powerful in that it is one pattern that actually speeds up operations, and it’s informative in that key OOP concepts are used and illuminated in it. It’s also one that has taken a while to really put together right—a process I’m still working on. Because this is a work in progress, I’d like to invite any and all developers with an interest in ActionScript 3.0 and design patterns to comment.<br />
<span id="more-21"></span><br />
<strong>The Flyweight Basics</strong></p>
<p>Imagine a situation where you want lots of buttons on the stage as a UI for information access. This is a pretty common situation and so a worthwhile effort if it works as it’s supposed to. You can use a component button from the <code>Button</code> class by importing it from <code>fl.controls</code> or build one using the <code>SimpleButton</code> class. The <em>intrinsic</em> features of a button are pretty similar—the way it works, looks and listens for events. However, the <em>extrinsic</em> features of buttons depend on their context. The position of a button on the stage, what it does with events, and what actions it calls forth are context dependent features. In a nutshell, the Flyweight separates the intrinsic from the extrinsic. The intrinsic state of an object is stored in the Flyweight. This information is context free and can be shared with all buttons. In fact, the Flyweight is a shared object. So instead of having several objects, you have one objects shared by many.</p>
<p>The example used by the <em>Gang of Four</em> is an application that uses the Flyweight is a word processor. Each character (glyph) is an object, and a word document contains a lot of character objects that are constantly reused. The intrinsic state of a character, like the letter “e”, is pretty standard. However, the extrinsic state changes with its position on the page (context). By reusing a single intrinsic state for each object, you can save a lot of time over creating a new one each time it’s needed on a page. So, the Flyweight stores the intrinsic state and then just changes the Flyweight’s extrinsic state to the desired position on the page. In the application planned for this article, instead of glyphs, we’ve got buttons, and just like the glyphs, all we need to do is the position them on an x/y grid.</p>
<p><strong>Flyweight Overview</strong></p>
<p>To get started, take a look at the Flyweight class diagram :</p>
<div id="attachment_2580" class="wp-caption alignnone" style="width: 495px"><img src="http://www.as3dp.com/wp-content/uploads/2007/06/FlyweightDiagramSmall.png" alt="Flyweight Class Diagram" title="FlyweightDiagramSmall" width="485" height="323" class="size-full wp-image-2580" /><p class="wp-caption-text">Flyweight Class Diagram</p></div>
<p>Like all of these diagrams, it takes some study to see what’s going on. (It took me a lot.) You’ll find some interesting relationships among the classes in the Flyweight pattern. First of all, the relationship between the <code>FlyweightFactory</code> and the <code>Flyweight</code> is one of <em>aggregation</em>. The factory class is the <em>aggregator</em> and the flyweight interface is the <em>aggregate</em>. You can think of the factory class owning the interface in the context of aggregation.</p>
<p>Second, the design pattern specifically includes the <strong>Client</strong> as a class. This means that the client has a responsibility in the pattern and references Participant classes. About half the patterns in the GoF book include a Client class. Chandima’s article on the Composite pattern also includes a Client class in the pattern. (If you see a gray Client class in a diagram that means that the Client is not part of the pattern, but is a handy tool to show which Participants interact with the Client class.)</p>
<p>Third, the pseudocode annotation associated with the <code>FlyweightFactory</code> class helps to give you a better idea of what is going on in the class. These annotations are extremely helpful in understanding exactly how the operations are implemented. However, as you have no doubt learned, when you implement in ActionScript 3.0 you need to design for ActionScript 3.0, which can be significantly different from say Java or C# where you will find most examples.</p>
<p><strong>An Abstract Flyweight in ActionScript 3.0</strong></p>
<p>One of the problems in creating any of the patterns described by GoF or in the Freemans’ wonderful book,  <em>HeadFirst Design Patterns</em>, is that they’re written in SmallTalk or Java. What’s more, they use some of the really cool built-in features of those languages that are not available in ActionScript 3.0. The Flyweight is no exception, plus the fact that it’s not expanded in  <em>HeadFirst Design Patterns</em>. So getting started has meant finding out how to work out a sensible alternative to hash tables (associative arrays) and decide whether to use an abstract class (which is not available in ECMAScript 4) or an interface, that is.</p>
<p>Fortunately for me, Jim Kremens, a top notch NYC Flex 2 developer, had an example of the Flyweight design pattern he shared with me. I made a few changes to suit my tastes, but it is basically Jim’s design. I swapped an interface with the “abstract class” (see Chandima’s  article, <em> Runtime Checks for Abstract Classes and Methods in ActionScript 3.0</em> on this blog.) Also, Jim used the <code>Array</code> class for creating an associative array, which works fine. However, the ActionScript 3.0 docs suggests using the<code> Object</code> class for associative arrays, and so I changed the <code>Array</code> class to an <code>Object </code>class. That’s not a lot of changes, and Jim’s design is strong enough to incorporate the changes with very little alternations to the code. Finally, I made the miniscule changes reflecting the switch from Flex 2 to Flash CS3 API.</p>
<p><strong>ActionScript Abstract Flyweight Example </strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p21code56'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2156"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
</pre></td><td class="code" id="p21code56"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Flyweight interface</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IFlyweight
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> build <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Flyweight Factory</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Factory
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> objects:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Factory <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			objects<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;X&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> FillObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Retrieve <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span>, ...<span style="color: #006600;">rest</span><span style="color: #66cc66;">&#41;</span>:IFlyweight
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>objects<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> :
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> :
					objects<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> = rest<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
					<span style="color: #b1b100;">break</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> objects<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Flyweight: Fill</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FillObject <span style="color: #0066CC;">implements</span> IFlyweight
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FillObject <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> build <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Build FillObject&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Flyweight: Stroke</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StrokeObject <span style="color: #0066CC;">implements</span> IFlyweight
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> StrokeObject <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> build <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Build StrokeObject&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Client class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyweightTest <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FlyweightTest <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> factory:Factory=<span style="color: #000000; font-weight: bold;">new</span> Factory  ;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> x:IFlyweight=factory.<span style="color: #006600;">Retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;X&quot;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//&quot;X&quot; already exists</span>
			x.<span style="color: #006600;">build</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> y:IFlyweight=factory.<span style="color: #006600;">Retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Y&quot;</span>,<span style="color: #000000; font-weight: bold;">new</span> StrokeObject  <span style="color: #66cc66;">&#41;</span>;
			y.<span style="color: #006600;">build</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> z:IFlyweight=factory.<span style="color: #006600;">Retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Z&quot;</span>,<span style="color: #000000; font-weight: bold;">new</span> FillObject  <span style="color: #66cc66;">&#41;</span>;
			z.<span style="color: #006600;">build</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see from both the code and the class diagram, the Client class (<code>FlyweightTest</code>) does not get stroke and fill objects directly from the concrete flyweights (stroke and fill) nor the <code>IFlyweight</code> interface. Instead it relies on the <code>Factory</code> class. To make this whole thing fly (no pun intended), Jim used the keyword, <code>…rest</code>. By placing the <code>…rest</code> keyword in a function’s parameter (called a <em>rest parameter</em>), the function will accept any number of comma-delimited arguments. In the <code>Retrieve</code> method in the <code>Factory</code> class, the rest parameter can be used to identify the specified concrete flyweight identified by the associative array’s key word. If the key is recognized as existing already in the <code>case</code> statement, the existing instance is returned. Otherwise the <code>Factory</code> cooks up a new instance. We can see this in the output:</p>
<p><code>Build FillObject<br />
Build StrokeObject<br />
Build FillObject</code></p>
<p>The first <code>FillObject</code> with the X key was already built in the <code>Factory</code> ; thus, it was not undefined. So, it just jumped out of the <code>switch</code> structure and returned the X object, which was a <code>FillObject</code>. The second key, Y,  was a <code>StrokeObject</code> that had not been defined, and so the factory built one. Likewise, with the Z key, no key named Z had been defined; so it created one, which happened to be another <code>FillObject</code>.</p>
<p>The concrete flyweights, both of which have implemented the <code>IFlyweight</code> interface, hold an intrinsic state. However, because we have no extrinsic state as a parameter, we may have a shared object, but we have no information for the extrinsic state. Further, do we really have a shared object? Remember that the concrete  <code>Flyweight</code> can be a shared object. So to test the example code for shared objects, we need to change the client (<code>FlyweightTest</code>) so that multiples of the shared object can be seen. To do this, I created a new concrete flyweight, <code>FlyButton</code>, representing the object that I eventually want to be used as a shared object. Likewise, I created a new client class to test the whole thing (<code>FlyweightShared</code>). The following two classes can be placed with the other participants and then tested in either Flash CS3 or Flex 2:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p21code57'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2157"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code" id="p21code57"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyButton <span style="color: #0066CC;">implements</span> IFlyweight
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FlyButton <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> build <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;I'm a shared button. I think...&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Client class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlyweightShared <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FlyweightShared <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> factory:Factory=<span style="color: #000000; font-weight: bold;">new</span> Factory;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> firstOne:IFlyweight=factory.<span style="color: #006600;">Retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ButtonFly&quot;</span><span style="color: #66cc66;">&#41;</span>;
			firstOne.<span style="color: #006600;">build</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> secondOne:IFlyweight=factory.<span style="color: #006600;">Retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ButtonFly&quot;</span><span style="color: #66cc66;">&#41;</span>;
			secondOne.<span style="color: #006600;">build</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> thirdOne:IFlyweight=factory.<span style="color: #006600;">Retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ButtonFly&quot;</span><span style="color: #66cc66;">&#41;</span>;
			thirdOne.<span style="color: #006600;">build</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In addition, we&#8217;ll need to make a slight change to the Factory class. Now it reads:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p21code58'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2158"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p21code58"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Flyweight Factory</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Factory
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> objects:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Factory <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			objects<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;ButtonFly&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> FlyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Retrieve <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span>, ...<span style="color: #006600;">rest</span><span style="color: #66cc66;">&#41;</span>:IFlyweight
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>objects<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> :
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> :
					objects<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span> = rest<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
					<span style="color: #b1b100;">break</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> objects<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This time, I got the same output for all of the three variables in the output window:</p>
<p><code>I'm a shared button. I think...<br />
I'm a shared button. I think...<br />
I'm a shared button. I think...<br />
</code></p>
<p>You may be wondering the same thing I am: &#8220;Is this really a shared object, or does it just instantiate three instances like you could do using <code>var button:Button = new Button()</code>?&#8221; If it&#8217;s a shared object, then we ought to have a much speedier and less processor-intensive structure. Otherwise, we&#8217;ve just succeeded in a convoluted way of creating a button. Tune in next time to see what&#8217;s up or leave a comment to straighten me out on this. In addition, we need to so something about that pesky extrinsic state, not to mention using a real button and not just a trace statement.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2007%2F06%2Fthe-actionscript-30-flyweight-saga-part-i%2F&amp;title=The%20ActionScript%203.0%20Flyweight%20Saga%3A%20Part%20I" id="wpa2a_8"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2007/06/the-actionscript-30-flyweight-saga-part-i/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
	</channel>
</rss>

