<?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; Delegation</title>
	<atom:link href="http://www.as3dp.com/category/delegation/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>Saturated Strategy 3: Extreme Composition in Delegation</title>
		<link>http://www.as3dp.com/2010/10/saturated-strategy-3-extreme-composition-in-delegation/</link>
		<comments>http://www.as3dp.com/2010/10/saturated-strategy-3-extreme-composition-in-delegation/#comments</comments>
		<pubDate>Mon, 04 Oct 2010 00:48:39 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Delegation]]></category>
		<category><![CDATA[Saturated Strategy]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=3704</guid>
		<description><![CDATA[One of the most sensible principles in the GoF tome is to favor composition over inheritance or, as Gamma, Helm, Johnson, and Vlissides put it: Favor object composition over class inheritance Object composition is an alternative to inheritance. However, they point to a very precise kind of inheritance that causes most of the problems they&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_3703" class="wp-caption alignleft" style="width: 210px"><img src="http://www.as3dp.com/wp-content/uploads/2010/10/CupcakeStrat3.png" alt="Favoring Delegation over Inheritance" title="CupcakeStrat3" width="200" height="200" class="size-full wp-image-3703" /><p class="wp-caption-text">Favoring Delegation over Inheritance</p></div> One of the most sensible principles in the GoF tome is to <em>favor composition over inheritance</em> or, as Gamma, Helm, Johnson, and Vlissides put it:</p>
<blockquote><p>Favor object composition over class inheritance</p></blockquote>
<p>Object composition is an alternative to inheritance. However, they point to a very precise kind of inheritance that causes most of the problems they&#8217;re talking about. For example, suppose we have a car object and we want a navigation system for it. The approach that GoF is concerned about is where the developer creates a subclass of the car object to include a navigation system or create a navigation subclass with added car functionality. Figure 1 shows the approach to inheritance they argue leads to tight coupling:<br />
<div id="attachment_3717" class="wp-caption alignright" style="width: 304px"><img src="http://www.as3dp.com/wp-content/uploads/2010/10/inheritance1.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Inheritance solutions to adding navigation&lt;/em&gt;" title="inheritance" width="294" height="213" class="size-full wp-image-3717" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Inheritance solutions to adding navigation</em></p></div> After using composition this kind of inheritance may seem heavy-handed, but it is the kind of reuse that Gamma, <em>et al</em>, are suggesting that is better handled by composition.  GoF use an example of a <strong>Window</strong> object  (a regular glass window—not one that Bill G. owns) obtaining a rectangle shape by becoming a subclass of a <strong>Rectangle</strong> class. In that way, the Window <strong><em>is a</em></strong> rectangle. The logic is that, since you have a rectangle anyway and windows are rectangles, just make a subclass of Rectangle and give it Window properties. In that way, you reuse all of the rich functionality of the rectangle without having to re-invent it in a new Window class independent of the Rectangle class. (Turn the page to find out more about composition and delegation.)<br />
<span id="more-3704"></span><br />
If you&#8217;re thinking that <em>composition is a much more natural way of adding functionality to objects</em>; then you probably understand design patterns better than you think. In looking at Figure 1, you can see how dependencies can pile up quickly if you subclass a car class with everything you want to add to it. Likewise, the alternative of subclassing a Navigation object and creating a Car class with car operations is even goofier. Using inheritance <em>does</em> reuse existing objects. The parent classes are reused. So it&#8217;s not a case where reuse has been ignored. It&#8217;s just that the inheritance approach to certain kinds of reuse piles up dependencies.</p>
<p>Briefly stated, composition is using other objects but not through inheritance. The Gang of Four explain composition thusly (p.19),</p>
<blockquote><p>New functionality is obtained by assembling or composing objects to get more complex functionality. Object composition requires that the objects being composed have well-defined interfaces.</p></blockquote>
<p> In the case of the Window example with <em>composition</em>, the Window <em>uses a</em> Rectangle rather than being a rectangle. GoF use the terminology <strong><em>has-a</em></strong> in contrast to<strong><em> is-a</em></strong>. A subclass <strong><em>is-a</em></strong> parent class (i.e., inherits its properties) while with composition, a single class (object) <strong><em>has-a</em></strong> (or even <strong><em>uses-a</em></strong>) different class (object) for additional functionality.</p>
<p><strong>Extreme Composition: Delegation</strong></p>
<p>Delegation is considered an <em>extreme example of composition</em> (p 21 GoF), and three patterns depend on it: <a href="http://www.as3dp.com/category/design-patterns/state/">State</a>, <a href="http://www.as3dp.com/category/design-patterns/visitor/">Visitor</a>, and <a href="http://www.as3dp.com/category/design-patterns/strategy-pattern/">Strategy</a>.</p>
<p>The purpose of delegation is to make composition as strong as inheritance for reuse. To do this, delegation requires two objects to handle a request. A <strong>receiving object</strong> receives a request and delegates operations to its <strong>delegate</strong>. The delegates are made up of concrete strategies in the Strategy pattern.</p>
<p>One way to look at the Strategy pattern is that the Client makes a request, the Context receives it and then delegates it to the ConcreteStrategy that provides the requested operational details. In some cases that will suffice to understand both delegation and the Strategy pattern. However, because GoF point to delegation specifically as a way to provide the same richness of content as inheritance, I think it deserves a bit more attention.</p>
<p><strong>From Inheritance to Delegation</strong></p>
<p>To get a different perspective on delegation, I started with a typical inheritance situation. Also, the request is coming from an object that uses the functionality as part of a more realistic object rather than a class I name Client.</p>
<p>To begin, consider the following Auto 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('p3704code4'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37044"><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="p3704code4"><pre class="actionscript" style="font-family:monospace;">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> Auto <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected const BODY:<span style="color: #0066CC;">Array</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Steel&quot;</span>,<span style="color: #ff0000;">&quot;lights&quot;</span>,<span style="color: #ff0000;">&quot;windows&quot;</span>,<span style="color: #ff0000;">&quot;doors&quot;</span>,<span style="color: #ff0000;">&quot;horn&quot;</span><span style="color: #66cc66;">&#41;</span>;
		protected const INTERIOR:<span style="color: #0066CC;">Array</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Seats&quot;</span>,<span style="color: #ff0000;">&quot;console&quot;</span>,<span style="color: #ff0000;">&quot;steering wheel&quot;</span>,<span style="color: #ff0000;">&quot;seat belts&quot;</span>,<span style="color: #ff0000;">&quot;air bags&quot;</span><span style="color: #66cc66;">&#41;</span>;
		protected const ENGINE:<span style="color: #0066CC;">Array</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Pistons&quot;</span>,<span style="color: #ff0000;">&quot;cam shaft&quot;</span>,<span style="color: #ff0000;">&quot;valves&quot;</span>,<span style="color: #ff0000;">&quot;gears&quot;</span>,<span style="color: #ff0000;">&quot;block&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Auto<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			honkHorn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			openWindows<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> honkHorn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>BODY<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span>+<span style="color: #ff0000;">&quot;: Toot, toot&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> openWindows<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>BODY<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>+<span style="color: #ff0000;">&quot;: Whosh, whosh&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></pre></td></tr></table></div>

<p>The Array objects set up as constants represent a set of properties for an automobile. The two methods represent behaviors that may be related to those properties. Focus on the properties and methods simply as features that are inherited by any child class.</p>
<p>Next,  add a GPS navigation system using inheritance:</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('p3704code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37045"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p3704code5"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AutoWithGPS <span style="color: #0066CC;">extends</span> Auto
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> AutoWithGPS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			gps<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> gps<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>INTERIOR<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>+<span style="color: #ff0000;">&quot;: Follow the map on the screen&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></pre></td></tr></table></div>

<p>When you test it, you can see that it acts like a typical subclass. It inherits the Auto properties represented by the use of INTERIOR[1]. It also toots the horn and opens the windows; two methods that are part of the parent class methods fired off in the constructor. So we can point to some re-usability in the design.</p>
<p>Suppose we wanted to use the auto GPS with an airplane? Or a boat? Airplane bodies are made of aluminum, and it would inherit the auto&#8217;s steel body; so we couldn&#8217;t use it for an airplane in this configuration. A boat with a steel body? Maybe. Big ships are made of steel, but small boats might go to the bottom with a steel body. We&#8217;d need to be careful with boats.</p>
<p>As we add more subclasses the dependencies pile up and our options become fewer and fewer. Now, let&#8217;s start with the Auto class and use delegation to adde GPS.</p>
<p>Begin by saving the original Auto class as  <strong>AutoDelegate.as</strong> in the same folder with the Strategy example from <a href="http://www.as3dp.com/2010/09/29/saturated-strategy-2-first-implementation/">Part 2</a> of this series. The listing below, adds GPS using delegation. The class still has all of the properties and methods of the Auto class without adding those properties to the GPS functionality. Also using either Flex or Flash, be sure to change the class to call, <strong>AutoDelegate</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('p3704code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37046"><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
</pre></td><td class="code" id="p3704code6"><pre class="actionscript" style="font-family:monospace;">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> AutoDelegate <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected const BODY:<span style="color: #0066CC;">Array</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Steel&quot;</span>,<span style="color: #ff0000;">&quot;lights&quot;</span>,<span style="color: #ff0000;">&quot;windows&quot;</span>,<span style="color: #ff0000;">&quot;doors&quot;</span>,<span style="color: #ff0000;">&quot;horn&quot;</span><span style="color: #66cc66;">&#41;</span>;
		protected const INTERIOR:<span style="color: #0066CC;">Array</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Seats&quot;</span>,<span style="color: #ff0000;">&quot;console&quot;</span>,<span style="color: #ff0000;">&quot;steering wheel&quot;</span>,<span style="color: #ff0000;">&quot;seat belts&quot;</span>,<span style="color: #ff0000;">&quot;air bags&quot;</span><span style="color: #66cc66;">&#41;</span>;
		protected const ENGINE:<span style="color: #0066CC;">Array</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Pistons&quot;</span>,<span style="color: #ff0000;">&quot;cam shaft&quot;</span>,<span style="color: #ff0000;">&quot;valves&quot;</span>,<span style="color: #ff0000;">&quot;gears&quot;</span>,<span style="color: #ff0000;">&quot;block&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> AutoDelegate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			honkHorn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			openWindows<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			getGPS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> honkHorn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>BODY<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span>+<span style="color: #ff0000;">&quot;: Toot, toot&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> openWindows<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>BODY<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>+<span style="color: #ff0000;">&quot;: Whosh, whosh&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> getGPS<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> gps:Context=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> StratGPS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</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>Now,  could we re-use the StratGPS object in an airplane or boat? Sure, it&#8217;s not dragging all of the auto&#8217;s baggage. We&#8217;re not bringing the steel body, horn or windows. We&#8217;re just delegating the GPS duties to the StratGPS concrete strategy.</p>
<p>In the above listing, the request is sent by the AutoDelegate object through the getGPS method. The getGPS() operation is a simple instantiation of a Context object that specifies which concrete context is required.</p>
<p>I hope this begins to shed some light on delegation. The Gang of Four seem to be very particular about what counts as <em>delegation</em> as opposed to just <em>object composition</em>. The acid test of true delegation is when an object passes along a reference to itself when passing along a request. The Context class does this. Because delegation can be tricky, the Gang of Four suggest that <em>you use it in highly stylized ways</em>. What (you may well ask&#8230;) is <em>a highly stylized way</em>?  Why it&#8217;s a standard design pattern that uses delegation, just like the Strategy pattern does.</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%2F2010%2F10%2Fsaturated-strategy-3-extreme-composition-in-delegation%2F&amp;title=Saturated%20Strategy%203%3A%20Extreme%20Composition%20in%20Delegation" 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/2010/10/saturated-strategy-3-extreme-composition-in-delegation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ActionsScript 3.0 Design Pattern Relations Part II: Aggregation</title>
		<link>http://www.as3dp.com/2010/01/actionsscript-3-0-design-pattern-relations-part-ii-aggregation/</link>
		<comments>http://www.as3dp.com/2010/01/actionsscript-3-0-design-pattern-relations-part-ii-aggregation/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 20:58:39 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Aggregation]]></category>
		<category><![CDATA[Class Relations]]></category>
		<category><![CDATA[Delegation]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2476</guid>
		<description><![CDATA[I&#8217;m tempted to say that aggregation is a stronger form of acquaintance, and that wouldn&#8217;t be far from wrong. In fact, in certain contexts it may be perfectly correct. Gamma, et al point out that the differences between the two is a matter of intent rather than explicit language mechanisms. That makes it a little [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_2683" class="wp-caption alignnone" style="width: 411px"><img src="http://www.as3dp.com/wp-content/uploads/2010/01/dpUMLagg2.png" alt="Aggregate Relations" title="dpUMLagg2" width="401" height="398" class="size-full wp-image-2683" /><p class="wp-caption-text">Aggregate Relations</p></div><br />
I&#8217;m tempted to say that <strong>aggregation</strong> is a stronger form of <strong>acquaintance</strong>, and that wouldn&#8217;t be far from wrong. In fact, in certain contexts it may be perfectly correct. Gamma, <em>et al </em>point out that the differences between the two  <em>is a matter of intent rather than explicit language mechanisms</em>. That makes it a little tricky simply to show some code and pronounce,<em> &#8220;See that&#8217;s what an aggregate looks like.&#8221;</em> So bear with me as we look at aggregate relations.</p>
<p><strong>Conjoined Participants</strong></p>
<p>As can be seen in the red arrow symbols in the Participant Relations diagram at the top of this post, an aggregate relation is indicated by an arrow with a diamond at its base. The GoF illustrate the relationship as shown in Figure 1:<br />
<div id="attachment_2486" class="wp-caption alignnone" style="width: 449px"><img src="http://www.as3dp.com/wp-content/uploads/2010/01/aggregation.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt;Aggregator and Aggregatee&lt;/em&gt;" title="aggregation" width="439" height="61" class="size-full wp-image-2486" /><p class="wp-caption-text"><em><strong>Figure 1: </strong>Aggregator and Aggregatee</em></p></div></p>
<p>The Aggregator creates an aggregate instance of the Aggregatee. As noted, GoF point out that no single code set can demonstrate what an aggregate relationship looks like, but I think that we can get a start by looking at a fairly simple relationship in the <a href="http://www.as3dp.com/2010/01/18/actionscript-3-0-state-design-pattern-an-aid-game/">State design</a> I created so that we could discuss both aggregate relations and delegation. (The <a href="http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/">Strategy design</a> also provides a good example of aggregation with delegation.)<br />
<span id="more-2476"></span><br />
In looking at the aggregate relations between the Context class and State interface in the  <a href="http://www.as3dp.com/2010/01/18/actionscript-3-0-state-design-pattern-an-aid-game/">State example<a/>,  we see the following aggregation:</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('p2476code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24768"><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
</pre></td><td class="code" id="p2476code8"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Context
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stoph:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> moveup:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> movedown:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> moveleft:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> moveright:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stateNow:State;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> chopper:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Context<span style="color: #66cc66;">&#40;</span> chopper:Sprite <span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">chopper</span>=chopper;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;State is here&quot;</span><span style="color: #66cc66;">&#41;</span>;
			moveup=<span style="color: #000000; font-weight: bold;">new</span> MoveUp<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			stoph=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			movedown=<span style="color: #000000; font-weight: bold;">new</span> MoveDown<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			moveleft=<span style="color: #000000; font-weight: bold;">new</span> MoveLeft<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			moveright=<span style="color: #000000; font-weight: bold;">new</span> MoveRight<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			stateNow=stoph;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> upMove<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			stateNow.<span style="color: #006600;">doMoveUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
...<span style="color: #0066CC;">end</span> snippet</pre></td></tr></table></div>

<p>So what&#8217;s getting aggregated? In this instance the entire State interface and all of its implementations (classes) and methods are aggregated in the Context class. All of the requests coming from the Client are delegated to the State through such aggregation.</p>
<p>At this point you may better understand GoF&#8217;s statement,</p>
<blockquote><p>Aggregation implies that an aggregate object and its owner have identical lifetimes.</p></blockquote>
<p>The aggregate object is that created in the Context and the owner is the State interface and implementations. The Context creates the aggregate object by gathering up all of the concrete states. When the Client makes a request, it uses an instance of the Context:</p>
<p> <strong><code>private var contextNow:Context=new Context(chopper);</code></strong></p>
<p>The instance, <strong><code>contextNow</code></strong>, essentially is the aggregate object. If you change the State implementations, it doesn&#8217;t matter because the Client makes the requests through the Context that creates an aggregate instance. That means if you change the implementations of the concrete States, everything still works because the requests are all through the Context class. As long as an aggregate relationship exists between participants in a design pattern, the aggregator creates an aggregate instance. With requests made through the aggregator, you decouple the request and the objects that carry out the requests. Further, through aggregation you can reuse objects all you want arranged in the way you want to employ them.</p>
<p><strong>Closely Related Participants</strong></p>
<p>You will find aggregate relationships in several design patterns, and the actual ActionScript 3.0 encoded aggregation has many faces. In coming to understand aggregation, take a look at several of the following classes that contain aggregation relationships.</p>
<ul>
<li><a href="http://www.as3dp.com/2009/03/17/actionscript-30-builder-design-pattern-part-i-controlling-creation/">Builder</a></li>
<li><a href="http://www.as3dp.com/2008/08/22/an-actionscript-bridge-design-pattern-flexibility-making-backdrops/">Bridge</a></li>
<li><a href="http://www.as3dp.com/2007/05/26/composite-pattern-book-part-1/">Composite</a></li>
<li><a href="http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/">Decorator</a></li>
<li><a href="http://www.as3dp.com/2007/09/18/the-actionscript-30-flyweight-saga-part-iii-aggregation-aggravation-stuff-on-the-stage-and-the-intrinsic-state/">Flyweight</a></li>
<li><a href="http://cdn.oreilly.com/digitalmedia/9780596528461/ActionScript3DesignPattern_ch07.pdf">Command</a></li>
<li><a href="http://www.as3dp.com/2008/12/14/actionscript-30-interpreter-design-pattern-a-postscript-tutor/">Interpreter</a></li>
<li><a href="http://www.as3dp.com/2008/01/27/actionscript-30-memento-design-pattern-flash-media-server-3-application/">Memento</a></li>
<li><a href="http://www.as3dp.com/2010/01/18/actionscript-3-0-state-design-pattern-an-aid-game/">State</a></li>
<li><a href="http://www.as3dp.com/2009/08/19/wrong-way-warrior-getting-flexibility-with-design-patterns—part-ii/">Strategy</a></li>
</ul>
<p>One of the more interesting uses of aggregation is where the child classes aggregate the parent class or interface. You can see this in the Composite and Decorator patterns. Likewise, you will see many different ways that the aggregation process is designed to structure the relationships between design pattern participants. By looking at the code in different examples aggregation you will begin to see what aggregation means.</p>
<p><strong>Aggregation for Change</strong></p>
<p>After posting the State example with the <strong>Helicopter</strong> (extension of Sprite class) jumping around the screen, we received several comments on improving the design. By having a State design pattern in place, that should not be too difficult to change the states for the Helicopter. The structure can remain the same, but not only could we substitute just about any sprite for the Helicopter, we could change the algorithms using the current interfaces without touching the fundamental design pattern. So in the next post, we&#8217;ll look at the Helicopter movement with an eye to making it more state-like while maintaining the current structure, even adding helper classes if we want. As always, we value your comments.</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%2F2010%2F01%2Factionsscript-3-0-design-pattern-relations-part-ii-aggregation%2F&amp;title=ActionsScript%203.0%20Design%20Pattern%20Relations%20Part%20II%3A%20Aggregation" 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/2010/01/actionsscript-3-0-design-pattern-relations-part-ii-aggregation/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ActionsScript 3.0 Design Pattern Relations Part I: Acquaintances</title>
		<link>http://www.as3dp.com/2010/01/actionsscript-3-0-design-pattern-relations-part-i-acquaintances/</link>
		<comments>http://www.as3dp.com/2010/01/actionsscript-3-0-design-pattern-relations-part-i-acquaintances/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 15:58:45 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Acquaintance]]></category>
		<category><![CDATA[Association]]></category>
		<category><![CDATA[Class Relations]]></category>
		<category><![CDATA[Delegation]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2374</guid>
		<description><![CDATA[This post is the first in a series where I hope to discuss all of the key relationships between classes in Design Patterns. To help identify posts in this series I&#8217;ve created a general diagram with all of the different kinds of relationships between classes. I did not include the boxes for pseudo-code or divide [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_2377" class="wp-caption none" style="width: 411px"><img src="http://www.as3dp.com/wp-content/uploads/2010/01/dpUML.png" alt="Class Relations" title="dpUML" width="401" height="398" class="size-full wp-image-2377" /><p class="wp-caption-text">Class Relations</p></div>This post is the first in a series where I hope to discuss all of the key relationships between classes in Design Patterns. To help identify posts in this series I&#8217;ve created a general diagram with all of the different kinds of relationships between classes. I did not include the boxes for pseudo-code or divide the classes into areas for operations and properties. The purpose is to focus on the relationships in this series. You may have noticed that the chart is titled, <strong>Participant Relations</strong>, and by <em>Participant</em>, I&#8217;m referring to any class or interface in a design pattern using the Gang of Four&#8217;s terminology. In order to help you quickly see the current relationship topic, the Participant Relations diagram shows the currently discussed relations in red.</p>
<p><strong>UML Controversy: Tempest in a Teapot</strong></p>
<p>In this and subsequent posts, I&#8217;m sticking with the notations used by GoF. If you&#8217;re into UMLs, you&#8217;ll know that GoF actually use a few non-standard notations, but I think that by using the ones GoF use our discussion will be less confusing. If you&#8217;re interested in a more standardized UML notation take a look at Judith Bishop&#8217;s book,<em> C# 3.0 Design Patterns</em> (O&#8217;Reilly) on pp.4-5.</p>
<p>GoF discuss abstract classes and interfaces as <strong><em>interfaces</em></strong>. So, in an example where an abstract class is used, you&#8217;ll see an italicized class name. If an interface is used, you&#8217;ll see the same thing. In some respects this can be quite helpful in that when you see any italicized text in a class diagram you&#8217;ll think <em>interface</em> and know that you can develop your own design pattern using either an abstract class or interface.</p>
<p>The inheritance symbol, that we&#8217;ll be covering in detail in a later post, is used to denote both <em>inheritance</em> (extends) and <em>implementation</em> (implements). This is consistent with GoF&#8217;s interchangable use of abstract classes and interfaces and their notations. So bear with me on these idiosyncrasies and help focus on the relationships between participants.<br />
<span id="more-2374"></span><br />
<strong>Let&#8217;s Get Acquainted</strong></p>
<p>When one class holds a reference to another class, we can represent that by a line with an arrow pointing to the class that it <em>has the </em>reference to as shown in the Participant Relations diagram between Class A and the Interface/Abstract Class. If one class holds a reference to many objects in another class, a solid circle is placed at the tip of the arrow as shown between Class D and Class E.</p>
<p>That&#8217;s easy enough to see, but the real question is <em>What&#8217;s going on</em>? In other words, why would one class hold a reference to another? The answer lies at the core of Design Patterns: <em>reuse</em>. Class A is using the the Interface/Abstract class in some manner shape or form. Instead of creating its own interface, it&#8217;s reusing another one.</p>
<p>If you can remember that, you&#8217;re 90% of the way to understanding Design Patterns. You may recall the second principle of Design Patterns is<br />
<blockquote>Favor object composition over class inheritance.</p></blockquote>
<p> The relationship indicates that Class A <em>has an</em> interface. It doesn&#8217;t implement the interface, but through composition <em>has the</em> interface. Such a relationship is an <em>acquaintance</em> one. That means <em>it has reused an existing object through composition.</em></p>
<p>In code, an acquaintance relationship can be many things. For example, consider the <a href="http://www.as3dp.com/category/design-patterns/prototype/">Prototype</a> pattern covered in this blog. In this pattern the Client asks a prototype to clone itself. The pertinent code looks like this:</p>
<p><strong><code>private var tDog:Prototype;<br />
tDog = new Dog();<br />
cDog = tDog.clone(tDog);</code></strong></p>
<p>You may be thinking, <em>That code creates an instance of the Dog class. Shouldn&#8217;t that be a <strong>creates</strong> relationship?</em> If you look closely, you will see that it does not create an instance of Prototype but rather of Dog. The acquaintance arrow points to the Prototype interface; so, while it does instantiate a class, it is through the acquaintance with Prototype and not directly with Dog.</p>
<p>At this point, it&#8217;s very easy for the  satellite  to spin out of orbit when it comes to differentiating between an <em>object reference</em> and the <em>creation</em> of one class by another. However, as you look at more and more implementations of design patterns in code, you&#8217;ll see that where you have an <strong>acquaintance</strong> relationship, you&#8217;ll find that one class is using the other class through composition and not just creation. Using composition allows one class to incorporate the interface of another class and use its properties and methods. To use those properties and methods, the class that has another class through composition needs some kind of instantiation.</p>
<p><strong>Think Composition</strong></p>
<p>A lot of problems understanding participant <strong>relations</strong> in design patterns can be solved if you keep this in mind: <em>Both inheritance and composition reuse a class interface</em>. When you think <em>composition</em>, you almost force your code into good OOP. If you don&#8217;t use composition, what are your alternatives?</p>
<ol>
<li>A big sequential program where you put all of your operations into one huge file. (Bad) </li>
<li>A big procedural program where call procedures as needed in one huge file. (Not good) </li>
<li>A big mother of all classes that spawns subclasses like of black widow laying thousands of eggs. (Scary)</li>
</ol>
<p>Composition is favored because it is more flexible:<br />
<blockquote>Composition is where, <em>new functionality is obtained by assembling or <strong>composing</strong> objects to get more complex functionality</em>.</p></blockquote>
<p> I use the term <em>flexible</em> in this context to mean that composition is defined dynamically at run-time and helps keep each class encapsulated and to focus on its single task. Think of this—salt and pepper. Salt has one function and pepper has another. In composition you can select one or the other or both. With inheritance from class Spices, the salt and pepper may get mixed together, which may be fine and save time in some instances. However, sometimes you just want one or the other. You want each encapsulated and you want them when you need them; not cooked into the meal where you may get too much of one or the other or both.</p>
<p><strong>Delegation</strong></p>
<p>Delegation is a way to get the most out of composition. So, <em>What&#8217;s delegation?</em> According to Gamma, Helm, Johnson and Vissides, delegation requires two objects to handle a request.<em> A <strong>receiving object</strong> delegates operations to its <strong>delegate</strong></em>. For an example of where delegation is used extensively, take a look at the <a href="http://www.as3dp.com/category/design-patterns/visitor/">Visitor</a> design pattern. The operations performed on the concrete elements are always delegated to a Visitor object. When using delegation, a request to an object is forwarded to a delegate instead of by the object using an inherited operation. By using delegation, you can see that it makes it easy to compose behaviors at run-time and to change the way they&#8217;re composed. (When we discuss aggregate relations, you&#8217;ll hear more about delegation.)</p>
<p><strong>Many References</strong></p>
<p>In some patterns like the Observer (Chapter <img src='http://www.as3dp.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> and <a href="http://www.as3dp.com/category/design-patterns/visitor/">Visitor</a>, you will see the acquaintance arrow with the solid circle on the end. That means one object maintains many references another object. In looking at the discussion of the Visitor pattern, you can see the ObjectStructure class holds many references to the Element interface. Such multiple references are often handled by Array and Vector objects. For example, in the Visitor post, you will see:</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('p2374code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p237410"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p2374code10"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//From ObjectStructure class</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> elements:<span style="color: #0066CC;">Array</span>=<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
 <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> attach<span style="color: #66cc66;">&#40;</span>element:IElement <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
 <span style="color: #66cc66;">&#123;</span>
          elements.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>element<span style="color: #66cc66;">&#41;</span>;
 <span style="color: #66cc66;">&#125;</span>
....</pre></td></tr></table></div>

<p>That sets up the acquaintance between the ObjectStructure and the Element interface. Because it is a reference to an Array object, it holds a reference to all of its elements as well.</p>
<p><strong>Where to Find Acquaintance Relations in Design Patterns</strong></p>
<p>Of the 23 patterns in the Design Pattern catalog, only 7 have acquaintance relations separate from Client requests. They are:</p>
<ol>
<li>Proxy</li>
<li>Adapter (Object version only)</li>
<li>Chain of Responsibility (Self-reference to Handler interface)</li>
<li>Command</li>
<li>Mediator</li>
<li>Observer</li>
<li>Visitor</li>
</ol>
<p>The great majority of acquaintance relations in class diagrams are between the Client class and one of the design pattern participants. Some, like the <a href="http://www.as3dp.com/category/design-patterns/abstract-factory-pattern/">Abstract Factory</a> and <a href="http://www.as3dp.com/category/design-patterns/flyweight/">Flyweight</a> , have multiple acquaintance relations between the Client and other participants. Also, if you look at Client relations, you will see that they are primarily through an interface and not through concrete classes. (The Flyweight is an exception and the Visitor and Interpreter are partial exceptions.)</p>
<p>In the next post covering Design Pattern relations, we&#8217;ll look at aggregate relations and see how they compare to the ubiquitous acquaintance relations. By comparing them, we can better understand them both and what role each plays in the different design patterns.</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%2F2010%2F01%2Factionsscript-3-0-design-pattern-relations-part-i-acquaintances%2F&amp;title=ActionsScript%203.0%20Design%20Pattern%20Relations%20Part%20I%3A%20Acquaintances" 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/2010/01/actionsscript-3-0-design-pattern-relations-part-i-acquaintances/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

