<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Composite Pattern: Extending the Book to include Composite Pages (Part II)</title>
	<atom:link href="http://www.as3dp.com/2007/08/05/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com/2007/08/05/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/</link>
	<description>OOP for Flash, Flex and AIR</description>
	<pubDate>Mon, 08 Sep 2008 14:51:02 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
		<item>
		<title>By: chandima</title>
		<link>http://www.as3dp.com/2007/08/05/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comment-630</link>
		<dc:creator>chandima</dc:creator>
		<pubDate>Tue, 10 Jun 2008 03:22:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/2007/08/06/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comment-630</guid>
		<description>Hi CG, try executing the &lt;code&gt;removeChild&lt;/code&gt; before splicing the component from the _cvChildren array because there is a chance that 'cv' can get garbage collected as soon as it is spliced. The following sequence inside the loop should work:

&lt;pre lang="actionscript"&gt;
removeChild(cv);
trace("removed component: " + cv);
_cvChildren.splice(i,1);
break;
&lt;/pre&gt;

You are correct. Simply removing the composite should suffice for garbage collection, as long as there are no external references to any of its child components.</description>
		<content:encoded><![CDATA[<p>Hi CG, try executing the <code>removeChild</code> before splicing the component from the _cvChildren array because there is a chance that &#8216;cv&#8217; can get garbage collected as soon as it is spliced. The following sequence inside the loop should work:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">removeChild<span style="color: #66cc66;">&#40;</span>cv<span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;removed component: &quot;</span> + cv<span style="color: #66cc66;">&#41;</span>;
_cvChildren.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span>i,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">break</span>;</pre></div></div>

<p>You are correct. Simply removing the composite should suffice for garbage collection, as long as there are no external references to any of its child components.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CG</title>
		<link>http://www.as3dp.com/2007/08/05/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comment-629</link>
		<dc:creator>CG</dc:creator>
		<pubDate>Sat, 07 Jun 2008 11:44:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/2007/08/06/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comment-629</guid>
		<description>This book is superb!!

How would I remove a single, specific component from a composite?  This is the closest I've come:

&lt;pre lang="actionscript"&gt;
override public function remove(cv:CountryComponent):void {
	for (var i:uint=0; i &lt; _cvChildren.length; i++) {
		if (_cvChildren[i] == cv) {
			_cvChildren.splice(i,1);
			removeChild(cv);
			trace("removed component: " + cv);
		}
	}
}&lt;/pre&gt;

Also - re: your last reply, if we want to implement a "remove all" we can just remove the composite, correct?  or do I have to script a removeAll that loops through _cvChildren and runs remove() for each component?</description>
		<content:encoded><![CDATA[<p>This book is superb!!</p>
<p>How would I remove a single, specific component from a composite?  This is the closest I&#8217;ve come:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> remove<span style="color: #66cc66;">&#40;</span>cv:CountryComponent<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> i:uint=0; i <span style="color: #66cc66;">&lt;</span> _cvChildren.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_cvChildren<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> == cv<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			_cvChildren.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span>i,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>cv<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;removed component: &quot;</span> + cv<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></div></div>

<p>Also - re: your last reply, if we want to implement a &#8220;remove all&#8221; we can just remove the composite, correct?  or do I have to script a removeAll that loops through _cvChildren and runs remove() for each component?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chandima</title>
		<link>http://www.as3dp.com/2007/08/05/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comment-576</link>
		<dc:creator>chandima</dc:creator>
		<pubDate>Fri, 25 Apr 2008 20:28:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/2007/08/06/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comment-576</guid>
		<description>Hi Ted, quite an interesting question. I think that one would implement a true component to intentionally prevent it from having any children. If you are not quite sure, then all nodes should probably be composites.

A good example is the display list in Flash as it implements a composite pattern. The component nodes in the display list inherit from the &lt;code&gt;DisplayObject&lt;/code&gt; class. The composite nodes inherit from the &lt;code&gt;DisplayObjectContainer&lt;/code&gt; class. The &lt;code&gt;Sprite&lt;/code&gt;, &lt;code&gt;Loader&lt;/code&gt; and &lt;code&gt;Stage&lt;/code&gt; classes extend the &lt;code&gt;DisplayObjectContainer&lt;/code&gt; class as we want them to have child components as in the case of nested sprites. In contrast, the &lt;code&gt;Bitmap&lt;/code&gt;, &lt;code&gt;Shape&lt;/code&gt; and &lt;code&gt;StaticText&lt;/code&gt; classes extend the &lt;code&gt;DisplayObject&lt;/code&gt; class and are true leaf nodes as for example, we don't want to have nested objects inside a StaticText field.

You are absolutely correct on the second point. I erred on the side of caution as I wasn't quite sure of how (and when) Flash garbage collection worked. I wish I'd looked at Grant Skinner's article on mark sweeping which, clearly shows that an isolated collection of objects referencing each other will also get garbage collected.

http://gskinner.com/blog/archives/2006/06/as3_resource_ma.html

I was a little paranoid about leaving a node branch out there with nothing to do.

Glad you like the book. 

-Chandima</description>
		<content:encoded><![CDATA[<p>Hi Ted, quite an interesting question. I think that one would implement a true component to intentionally prevent it from having any children. If you are not quite sure, then all nodes should probably be composites.</p>
<p>A good example is the display list in Flash as it implements a composite pattern. The component nodes in the display list inherit from the <code>DisplayObject</code> class. The composite nodes inherit from the <code>DisplayObjectContainer</code> class. The <code>Sprite</code>, <code>Loader</code> and <code>Stage</code> classes extend the <code>DisplayObjectContainer</code> class as we want them to have child components as in the case of nested sprites. In contrast, the <code>Bitmap</code>, <code>Shape</code> and <code>StaticText</code> classes extend the <code>DisplayObject</code> class and are true leaf nodes as for example, we don&#8217;t want to have nested objects inside a StaticText field.</p>
<p>You are absolutely correct on the second point. I erred on the side of caution as I wasn&#8217;t quite sure of how (and when) Flash garbage collection worked. I wish I&#8217;d looked at Grant Skinner&#8217;s article on mark sweeping which, clearly shows that an isolated collection of objects referencing each other will also get garbage collected.</p>
<p><a href="http://gskinner.com/blog/archives/2006/06/as3_resource_ma.html" rel="nofollow">http://gskinner.com/blog/archives/2006/06/as3_resource_ma.html</a></p>
<p>I was a little paranoid about leaving a node branch out there with nothing to do.</p>
<p>Glad you like the book. </p>
<p>-Chandima</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ted H.</title>
		<link>http://www.as3dp.com/2007/08/05/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comment-565</link>
		<dc:creator>Ted H.</dc:creator>
		<pubDate>Tue, 22 Apr 2008 12:57:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/2007/08/06/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comment-565</guid>
		<description>Hi!  First, TERRIFIC book!  I am new to FLEX and Actionscript (come from a .NET background) and this book has really helped.

I have two questions for you re: the Composite Pattern - one about the pattern itself, one more Actionscript related.

First, I am creating a data structure that's essentially like a DOM-tree and I don't know at node-creation-time if the node "is" a branch or a leaf; so I'm making all my nodes branches (composites).  I figured that a leaf is a leaf by simple fact that it has no children, but then I was afraid I'd need to "convert" it to composite if the user decided later they wanted to add child nodes to it.

For example, take a bulleted list (...).  Most of the time the list item will just contain text (and be a leaf), but some of the time, the list item will contain lots of child nodes as well - a table, another list inside a table (which is wrapped in  and  and )...

Is this a common scenario with the Composite Pattern?  I've not read anyone else comment on this problem so I assume either I've got it wrong, it's not a common problem, or all my nodes are REALLY Composite nodes (even if they don't have Children) simply because they CAN have Children.

Any thoughts?

My second question regards removing nodes.  You recursively loop through the child axis to remove nodes to release memory.  I find this confusing. If I simple remove reference to a child node from it's parent (in the parent's child nodes array), does this not destroy the children in this axis? 

I mean, do you really need to go through and kill every child node individually to release memory?  If so, can you offer any more detail about this?  I was surprised to hear I needed to do the recursive loop (although maybe I just don't understand something there too).

Many thanks - again, terrific book (and BLOG!).
-Ted</description>
		<content:encoded><![CDATA[<p>Hi!  First, TERRIFIC book!  I am new to FLEX and Actionscript (come from a .NET background) and this book has really helped.</p>
<p>I have two questions for you re: the Composite Pattern - one about the pattern itself, one more Actionscript related.</p>
<p>First, I am creating a data structure that&#8217;s essentially like a DOM-tree and I don&#8217;t know at node-creation-time if the node &#8220;is&#8221; a branch or a leaf; so I&#8217;m making all my nodes branches (composites).  I figured that a leaf is a leaf by simple fact that it has no children, but then I was afraid I&#8217;d need to &#8220;convert&#8221; it to composite if the user decided later they wanted to add child nodes to it.</p>
<p>For example, take a bulleted list (&#8230;).  Most of the time the list item will just contain text (and be a leaf), but some of the time, the list item will contain lots of child nodes as well - a table, another list inside a table (which is wrapped in  and  and )&#8230;</p>
<p>Is this a common scenario with the Composite Pattern?  I&#8217;ve not read anyone else comment on this problem so I assume either I&#8217;ve got it wrong, it&#8217;s not a common problem, or all my nodes are REALLY Composite nodes (even if they don&#8217;t have Children) simply because they CAN have Children.</p>
<p>Any thoughts?</p>
<p>My second question regards removing nodes.  You recursively loop through the child axis to remove nodes to release memory.  I find this confusing. If I simple remove reference to a child node from it&#8217;s parent (in the parent&#8217;s child nodes array), does this not destroy the children in this axis? </p>
<p>I mean, do you really need to go through and kill every child node individually to release memory?  If so, can you offer any more detail about this?  I was surprised to hear I needed to do the recursive loop (although maybe I just don&#8217;t understand something there too).</p>
<p>Many thanks - again, terrific book (and BLOG!).<br />
-Ted</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chandima</title>
		<link>http://www.as3dp.com/2007/08/05/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comment-202</link>
		<dc:creator>chandima</dc:creator>
		<pubDate>Thu, 20 Sep 2007 20:56:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/2007/08/06/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comment-202</guid>
		<description>Thanks for the note. If you are interested in how events can cascade down a complex composite structure; For example, when a composite view is incorporated into a model-view-controller pattern, our MVC chapter 12 has some detail on that. The MVC chapter is available as a free download on Adobe DevNet:

http://www.adobe.com/devnet/actionscript/articles/ora_as3_design_patterns.html</description>
		<content:encoded><![CDATA[<p>Thanks for the note. If you are interested in how events can cascade down a complex composite structure; For example, when a composite view is incorporated into a model-view-controller pattern, our MVC chapter 12 has some detail on that. The MVC chapter is available as a free download on Adobe DevNet:</p>
<p><a href="http://www.adobe.com/devnet/actionscript/articles/ora_as3_design_patterns.html" rel="nofollow">http://www.adobe.com/devnet/actionscript/articles/ora_as3_design_patterns.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pdazzle</title>
		<link>http://www.as3dp.com/2007/08/05/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comment-201</link>
		<dc:creator>Pdazzle</dc:creator>
		<pubDate>Thu, 20 Sep 2007 15:07:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/2007/08/06/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comment-201</guid>
		<description>Great book / site. 

How about a part three dealing with handling events in the composite pattern? Would be really helpful.</description>
		<content:encoded><![CDATA[<p>Great book / site. </p>
<p>How about a part three dealing with handling events in the composite pattern? Would be really helpful.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
