<?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>Ruben's blog &#187; namespaces</title>
	<atom:link href="http://www.rubenswieringa.com/blog/tag/namespaces/feed" rel="self" type="application/rss+xml" />
	<link>http://www.rubenswieringa.com/blog</link>
	<description>Ruben Swieringa on Actionscript and a whole lot of other stuff..</description>
	<lastBuildDate>Mon, 15 Feb 2010 09:35:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A different use for custom namespaces (2/2)</title>
		<link>http://www.rubenswieringa.com/blog/a-different-use-for-custom-namespaces-2</link>
		<comments>http://www.rubenswieringa.com/blog/a-different-use-for-custom-namespaces-2#comments</comments>
		<pubDate>Thu, 03 Sep 2009 19:12:22 +0000</pubDate>
		<dc:creator>Ruben</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[namespaces]]></category>

		<guid isPermaLink="false">http://www.rubenswieringa.com/blog/?p=161</guid>
		<description><![CDATA[Note: Note that to keep the focus on the abstract/theoretical part of the subject I seperated the content over two seperate posts (of which this is the second). The previous explains the idea while this one shows a possbible use-case. I suggest you read the first post if you haven't already, it'll make things a [...]]]></description>
			<content:encoded><![CDATA[<p>Note: Note that to keep the focus on the abstract/theoretical part of the subject I seperated the content over two seperate posts (of which this is the second). <a href="http://www.rubenswieringa.com/blog/a-different-use-for-custom-namespaces-1">The previous</a> explains the idea while this one shows a possbible use-case. I suggest you read the first post if you haven't already, it'll make things a lot more clear.</p><p>This post provides some example-code for the concept presented in my <a href="http://www.rubenswieringa.com/blog/a-different-use-for-custom-namespaces-1">previous post</a>, please note that this post serves only to add some concrete material to the abstract idea presented in the previous post and that the focus should be on the latter rather than the former. This post is not at all about best practices or good/bad code-architecture.</p>
<p><span id="more-161"></span></p>
<p>I first started thinking about using namespaces for overriding methods when at the end of july I was talking to Tommy about a problem he was having. He had overridden <code>addChild()</code> in some distant <code>DisplayObject</code> subclass but in some situations still needed to use the original implementation of the method. Because for this he used <code>super.addChild()</code> he was forced to put a lot of different logic in that one single subclass.</p>
<p>The implementation of the double-overriding popped up when I suggested to store the original <code>addChild()</code> functionality in a duplicate function defined with a custom namespace and the same name, and then put the rest of the functionality in a subclass of the class that would override <code>addChild()</code>:</p>
<pre class="actionscript"><span style="color: #808080; font-style: italic;">// DisplayObjectOverridden.as:</span>
package
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">DisplayObject</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> DisplayObjectOverridden <span style="color: #0066CC;">extends</span> Sprite
   <span style="color: #66cc66;">&#123;</span>
      <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DisplayObjectOverridden <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#123;</span>
         <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addChild<span style="color: #66cc66;">&#40;</span>child:DisplayObject<span style="color: #66cc66;">&#41;</span>:DisplayObject
      <span style="color: #66cc66;">&#123;</span>
         <span style="color: #808080; font-style: italic;">// custom functionality</span>
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      original <span style="color: #000000; font-weight: bold;">function</span> addChild <span style="color: #66cc66;">&#40;</span>child:DisplayObject<span style="color: #66cc66;">&#41;</span>:DisplayObject
      <span style="color: #66cc66;">&#123;</span>
         <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">super</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>child<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>
<pre class="actionscript"><span style="color: #808080; font-style: italic;">// OtherFunctionality.as:</span>
package
<span style="color: #66cc66;">&#123;</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> OtherFunctionality <span style="color: #0066CC;">extends</span> DisplayObjectOverridden
   <span style="color: #66cc66;">&#123;</span>
      <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> OtherFunctionality <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> normalChild:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
         addChild<span style="color: #66cc66;">&#40;</span>normalChild<span style="color: #66cc66;">&#41;</span>;
&nbsp;
         <span style="color: #000000; font-weight: bold;">var</span> specialChild:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
         original::addChild<span style="color: #66cc66;">&#40;</span>specialChild<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>
<pre class="actionscript"><span style="color: #808080; font-style: italic;">// original.as:</span>
package
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #0066CC;">public</span> namespace original;
<span style="color: #66cc66;">&#125;</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.rubenswieringa.com/blog/a-different-use-for-custom-namespaces-2/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A different use for custom namespaces (1/2)</title>
		<link>http://www.rubenswieringa.com/blog/a-different-use-for-custom-namespaces-1</link>
		<comments>http://www.rubenswieringa.com/blog/a-different-use-for-custom-namespaces-1#comments</comments>
		<pubDate>Thu, 03 Sep 2009 19:11:38 +0000</pubDate>
		<dc:creator>Ruben</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[namespaces]]></category>

		<guid isPermaLink="false">http://www.rubenswieringa.com/blog/?p=156</guid>
		<description><![CDATA[Note: Note that to keep the focus on the abstract/theoretical part of the subject I seperated the content over two seperate posts. The first (this one) explains the idea while the second shows a possbible use-case and some example-code.When namespaces were first introduced in Actionscript 3 I thought it was all pretty cool, but still [...]]]></description>
			<content:encoded><![CDATA[<p>Note: Note that to keep the focus on the abstract/theoretical part of the subject I seperated the content over two seperate posts. The first (this one) explains the idea while <a href="http://www.rubenswieringa.com/blog/a-different-use-for-custom-namespaces-2">the second</a> shows a possbible use-case and some example-code.</p><p>When <a href="http://www.rubenswieringa.com/blog/namespaces-in-actionscript3">namespaces</a> were first introduced in Actionscript 3 I thought it was all pretty cool, but still I sort of had a difficult time thinking of actual use-cases.</p>
<p>What I initially would have liked to use custom namespaces for was to restrict access to certain properties/methods, to only be accessible for certain classes, in the way that <code>private</code> and <code>protected</code> enable you to -- unfortunately there is no simple way of defining who can import your namespace, consequently rendering namespaces somewhat useless for this particular use-case.</p>
<p>Then some time ago it struck me that rather than defining new members with a custom namespace, that you could of course also enhance an existing method instead (I admit this might seem like a no-brainer, but it took me a while to bump into).</p>
<p>Let's say you have a subclass that for regular (outside) usage needs to override a method in the superclass, but itself still needs to use the original (super-) method. You would probably just override the method in the subclass and use <code>super.method()</code> wherever you needed to use the original functionality.<br />
Now let's say that you need to make a subclass <em>of</em> your subclass, which would also require access to the original functionality in said method. All of a sudden <code>super.method()</code> will not work for you anymore.</p>
<p>Here's where namespaces start coming in handy, while still simply overriding the method in the first subclass, you also implement a function with the same name <em>but</em> with your own namespace (for example <em>"original"</em>). Then whenever you need to call the original implementation you can call <code>original::method()</code>, regardless of how deep down in the inheritance-chain you are.</p>
<p>This is only one use-case, you could for instance also use this concept to work around the nasty <a href="http://www.rubenswieringa.com/blog/ambiguous-reference-bug-for-namespaces-in-flex">ambiguous-reference bug</a> you get (or used to get) when declaring a getter and a setter with different (native) namespaces.</p>
<p>To keep the focus on the above described concept I put the example-code in <a href="http://www.rubenswieringa.com/blog/a-different-use-for-custom-namespaces-2">a seperate post, feel free to read on</a> though.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rubenswieringa.com/blog/a-different-use-for-custom-namespaces-1/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
