<?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; Aggregation</title>
	<atom:link href="http://www.as3dp.com/category/class-relations/aggregation/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>ActionScript 3.0 Saturated Bridge Design Pattern 2: Bridge Clock Implementation</title>
		<link>http://www.as3dp.com/2011/03/actionscript-3-0-saturated-bridge-design-pattern-2-bridge-clock-implementation/</link>
		<comments>http://www.as3dp.com/2011/03/actionscript-3-0-saturated-bridge-design-pattern-2-bridge-clock-implementation/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 00:32:50 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Aggregation]]></category>
		<category><![CDATA[Bridge]]></category>
		<category><![CDATA[Saturated Bridge]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=4947</guid>
		<description><![CDATA[Aggregation Magic In discussing aggregation, the Gang of Four left a lot of loose ends, and the relationship can be difficult to grasp. It&#8217;s something like an acquaintance but is stronger. Nevertheless, acquaintances can be just as strong but not be aggregations. So is it really that important to distinguish between aggregation and acquaintances? In [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_4858" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/03/MoonBridge.png" alt="Independent Variationby Decoupling Abstractions and Implementations" title="MoonBridge" width="250" height="213" class="size-full wp-image-4858" /><p class="wp-caption-text">Make an Orthogonally Designed Alarm Clock</p></div><strong>Aggregation Magic</strong></p>
<p>In discussing <strong>aggregation</strong>, the Gang of Four left a lot of loose ends, and the relationship can be difficult to grasp. It&#8217;s something like an <strong>acquaintance</strong> but is stronger. Nevertheless, acquaintances can be just as strong but not be aggregations. So is it really that important to distinguish between aggregation and acquaintances? In most cases the distinction is important  because while many similarities exist between them (just as with men and women), the differences change the character of the relationship.  Gamma <em>et al</em> point out,</p>
<blockquote><p>Aggregation implies that an aggregate object and its owner have identical lifetimes, (p. 22)</p></blockquote>
<p>but that description brings up other questions. We can assume that an aggregate object is the combination of two objects bound through aggregation. However, what is its <em>owner</em>? In <a href="http://www.as3dp.com/2011/03/06/actionscript-3-0-saturated-abstract-bridge-intention/">Part I</a> of this series, you saw that the Bridge pattern is made up in part of an orthogonal component bound by aggregation. That&#8217;s pretty clear, but how is that component <em>owned</em>? Does that mean the parts that make up the component are the owners? If one or the other of the aggregated objects (classes) ceases to be; so will the other one. Thus, the parts are the owners. However, another way of looking at ownership in an aggregate relationship is by examining it parts.</p>
<p>The aggregation relationship is characterized by the Aggregator and Aggregatee 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>Does the Aggregator own Aggregatee?</em></p></div></p>
<p>In this model, it would appear that the aggregator is the &#8220;owner.&#8221; It defines the relationship by holding a reference to the aggregatee.</p>
<p>The Orthogonal Bridge</p>
<p>The &#8220;aggregateinstance&#8221; in Figure 1 is the &#8220;bridge&#8221; in the Bridge pattern, and it is the orthogonal component discussed in detail in Part I of this series. The next step in understanding the Bridge design pattern is to implement it. The alarm clock provides a clear way to see the relationship between objects linked by an aggregation that makes up an orthogonal component. Figure 2 show the files for the implementation arranged in a File Diagram:<br />
<div id="attachment_4949" class="wp-caption alignnone" style="width: 494px"><img src="http://www.as3dp.com/wp-content/uploads/2011/03/BridgeFileDiagram.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt;Bridge File Class Diagram&lt;/em&gt;" title="BridgeFileDiagram" width="484" height="221" class="size-full wp-image-4949" /><p class="wp-caption-text"><em><strong>Figure 2: </strong>Bridge File Class Diagram</em></p></div><br />
This Bridge example is quite simple and easy to understand as a Bridge pattern. The two interfaces are made up of an abstract class (IAlarmClock) and an interface (IAlarm.) You can think of the physical alarm clock as an instrument that keeps time and the alarm as a sound it makes to generate an alert. The clock and its alarm can vary independently but work in conjunction. The clock has a time-keeping function that can be used to call the alarm when needed. In the example, the clock implementation is that of an analog clock that rings a bell and a clock radio that plays Ride of the Valkyrires (Wagner&#8217;s Walkürenritt). It&#8217;s easy to add a different clock implementation (a Cuckoo clock, for example) or a different alarm—a rocket ship blasting off if you wish. The following two buttons will download and Play the example for you:<br />
<a href="http://www.sandlight.com/bridge/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://poobah.mwd.hartford.edu/wsanders/bridge/BridgeClock.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p>The clocks do not update, but they should display your accurate local time. You can easily put a Timer object in the program to make the clocks work like running clocks.  Likewise, while I did not place an alarm setting function in the clocks, I placed an alarm testing button.<em> That&#8217;s so you wouldn&#8217;t have to sit around waiting for the alarm to ring.</em> However, it wouldn&#8217;t be difficult to do so. In fact if any of you want to place a running clock in the pattern by extending another implementation from the <strong>IAlarmClock</strong> abstract class, I&#8217;d very much like to see it, and I&#8217;ll display it on the next blog entry in this series—same with implementations to the <strong>IAlarm</strong> interface. Okay so where do we start with implementing this pattern? Read on.<br />
<span id="more-4947"></span></p>
<p><strong>The Reversed Engineered Implementation of the Alarm</strong></p>
<p>We might as well start at the ending. In fact I knew where I was going (driving backwards) because I had a good sense of the Bridge pattern; so it wasn&#8217;t starting without a roadmap. I wanted to start with an <em>alarm</em> implementation. Getting the sounds was easy (check out <a href="http://www.gutenberg.org/ebooks/10177">Project Gutenberg</a> and the old standby, <a href="http://www.flashkit.com/">Flash Kit</a> for mp3 sounds). However, I always have to go back and re-learn the procedure for playing sounds since I don&#8217;t use sounds that much. The following implementation was used for both with the only difference being the MP3 selected:</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('p4947code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p49477"><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
</pre></td><td class="code" id="p4947code7"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Clock radio IAlarm implementation</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #0066CC;">Sound</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #006600;">SoundChannel</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RideAlarm <span style="color: #0066CC;">implements</span> IAlarm
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> aChannel:SoundChannel;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> aSound:<span style="color: #0066CC;">Sound</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> aRequest:URLRequest;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> alarmPlay<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			aSound=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			aChannel=<span style="color: #000000; font-weight: bold;">new</span> SoundChannel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			aRequest=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;alarms/rideOfValkyries.mp3&quot;</span><span style="color: #66cc66;">&#41;</span>;
			aSound.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>aRequest<span style="color: #66cc66;">&#41;</span>;
			aChannel=aSound.<span style="color: #0066CC;">play</span><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> alarmOff<span style="color: #66cc66;">&#40;</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>aChannel<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				aChannel.<span style="color: #0066CC;">stop</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>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s pretty simple. All I really needed were methods to play the alarm and to shut it off. From here I backed into the IAlarm interface:</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('p4947code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p49478"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p4947code8"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Alarm interface</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IAlarm
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> alarmPlay<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">function</span> alarmOff<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I don&#8217;t mean to sound like any idiot can get this stuff, but sometimes we make things harder for ourselves than we should—especially when it comes to design patterns. (Yes, I know this is a simple example. It&#8217;s supposed to be, but if you can grasp the pattern concept, then you&#8217;re good to go for more complex implementations of the same pattern.)</p>
<p><strong>Alarm Clocks</strong></p>
<p>Early on I decided that the alarm triggers in the clocks would be buttons rather than a timing trigger. The concepts of triggering the alarm are identical—an event does it. It really doesn&#8217;t matter whether the event is one caused by a Date method or MouseEvent; so I used a MouseEvent so that I could play with the clock and the alarm during development.</p>
<p>For the clocks, I definitely wanted to start with the abstract class from which the clock implementations would come from. First, I had to make sure that the abstract class held a reference to the IAlarm interface. Second, I had a good idea of what methods were needed even though their implementation would vary. The following listing shows the abstract class, IAlarmClock:</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('p4947code9'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p49479"><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="p4947code9"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Abstract class for Alarm Clock</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>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IAlarmClock <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> timeNow:<span style="color: #0066CC;">Date</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> alarm:IAlarm;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> curTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</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> doAlarm<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;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</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> offAlarm<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;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&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>I knew that the <strong>Date</strong> object would be needed for the clocks because the class has properties for the hours and minutes needed for the clock hands and &#8220;digital&#8221; display. The <strong>IAlarm</strong> instance, <strong>alarm</strong>, established an aggregate relationship between the abstract class and alarm interface.</p>
<p>The implementations of the <strong>curTime()</strong> method were very close to being constructor functions. The idea of the method originally had been to update the time in hours and minutes from the parent class <strong>timeNow</strong> property. However, I ended up using it for building the clock and setting the hands or &#8220;digital&#8221; values on the clocks as well as getting the time and instantiating the alarm implementations.</p>
<p>Unlike the alarm implementations, the clock implementations were very different. One was used to set up an analog clock and the other a &#8220;clock radio.&#8221; I had drawn the face and the clock hands and saved them as Sprite classes, <strong>ClockFace</strong>,<strong> Hour</strong> and <strong>Minute</strong>. The following two listings show these implementations:</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('p4947code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p494710"><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
</pre></td><td class="code" id="p4947code10"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Analog clock: IAlarmClock implementation</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> BellClock <span style="color: #0066CC;">extends</span> IAlarmClock
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> minHand:Minute=<span style="color: #000000; font-weight: bold;">new</span> Minute<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> hrHand:Hour=<span style="color: #000000; font-weight: bold;">new</span> Hour<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cFace:ClockFace=<span style="color: #000000; font-weight: bold;">new</span> ClockFace<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> clockUp:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> adjust:uint;
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> curTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			alarm=<span style="color: #000000; font-weight: bold;">new</span> DingAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cFace.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>;
			hrHand.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">200</span>,hrHand.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			hrHand.<span style="color: #006600;">rotation</span>=<span style="color: #66cc66;">&#40;</span>timeNow.<span style="color: #006600;">hours</span>+<span style="color: #66cc66;">&#40;</span>timeNow.<span style="color: #006600;">minutes</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">30</span>
			minHand.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">200</span>,minHand.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			minHand.<span style="color: #006600;">rotation</span>=timeNow.<span style="color: #006600;">minutes</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">6</span>;
			clockUp.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>cFace<span style="color: #66cc66;">&#41;</span>;
			clockUp.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>hrHand<span style="color: #66cc66;">&#41;</span>;
			clockUp.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>minHand<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> clockUp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> doAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			alarm.<span style="color: #006600;">alarmPlay</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> override <span style="color: #000000; font-weight: bold;">function</span> offAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			alarm.<span style="color: #006600;">alarmOff</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>The clock radio implementation uses a Sprite backdrop from the Library. The &#8220;digital&#8221; output is a text field with the current time (in hours and minutes). It is set up in a 12-hour loop even though no &#8220;AM&#8221; and &#8220;PM&#8221; indicators are used. (Think of it as another opportunity for improvement.) The important feature is that it uses the same interface (abstract class parent) and instantiates the concrete alarm just like the analog clock.</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('p4947code11'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p494711"><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
</pre></td><td class="code" id="p4947code11"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Clock radio implementation of IAlarmClock</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</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> fl.<span style="color: #006600;">motion</span>.<span style="color: #006600;">AdjustColor</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RadioClock <span style="color: #0066CC;">extends</span> IAlarmClock
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> clockUp:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> clockRadio:RadioFace=<span style="color: #000000; font-weight: bold;">new</span> RadioFace<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> timeField:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> timeFormat:<span style="color: #0066CC;">TextFormat</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> hr:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mn:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> fullTime:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> adjust:uint;
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> curTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			alarm=<span style="color: #000000; font-weight: bold;">new</span> RideAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			timeNow.<span style="color: #006600;">hours</span><span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">12</span> ? adjust=timeNow.<span style="color: #006600;">hours</span>-<span style="color: #cc66cc;">12</span>:adjust=timeNow.<span style="color: #006600;">hours</span>;
			hr=<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>adjust<span style="color: #66cc66;">&#41;</span>;
			mn=<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>timeNow.<span style="color: #006600;">minutes</span><span style="color: #66cc66;">&#41;</span>;
			mn.<span style="color: #0066CC;">length</span>==<span style="color: #cc66cc;">1</span> ? mn=<span style="color: #ff0000;">&quot;0&quot;</span>+ mn : mn=mn;
			fullTime=hr+<span style="color: #ff0000;">&quot;:&quot;</span>+mn;
			timeField.<span style="color: #0066CC;">autoSize</span>=TextFieldAutoSize.<span style="color: #0066CC;">RIGHT</span>;
			timeFormat.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;OCR A Std&quot;</span>;
			timeFormat.<span style="color: #0066CC;">color</span>=0x00ff00;
			timeFormat.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">72</span>;
			timeField.<span style="color: #006600;">defaultTextFormat</span>=timeFormat;
			timeField.<span style="color: #0066CC;">text</span>=fullTime;
			timeField.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">95</span>,timeField.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">75</span>;
			clockUp.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>clockRadio<span style="color: #66cc66;">&#41;</span>;
			clockUp.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>timeField<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> clockUp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> doAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			alarm.<span style="color: #006600;">alarmPlay</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> override <span style="color: #000000; font-weight: bold;">function</span> offAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			alarm.<span style="color: #006600;">alarmOff</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 the code, it&#8217;s very flexible but forms a strong orthogonal component. Changing an alarm requires only that the name of the MP3 file is changed. A quick cut-and-paste and you have another alarm sound or tune. Changing clocks requires a new clock face, but adding another can easily fit in with the other interacting participants in the pattern, and <em>that&#8217;s what is important</em>.</p>
<p><strong>Making Requests through the Client</strong></p>
<p>The Bridge includes the Client as an <em>implicit</em> class participant in the pattern. In this example, the Client is more like a sales associate who&#8217;s job it is to display the wares—the clocks and their alarms. Most of the code in the Client is creating the buttons and event handler methods. The methods include a good number conditional statements that check to see if the an extant instance of the clock. In testing, I found that the program would crash faster than a drunk driver if the current usage of an object were not first checked. In a non-demonstrative application, all the client would do would be to instantiated the selected clock and let it take care of the alarm event.</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('p4947code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p494712"><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
</pre></td><td class="code" id="p4947code12"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Client</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>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> aClock:IAlarmClock;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> selectBell:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> selectRadio:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> testAlarm:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> shutOff:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> clockNow:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnWidth:uint=shutOff.<span style="color: #0066CC;">width</span>+<span style="color: #cc66cc;">10</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>clockNow<span style="color: #66cc66;">&#41;</span>;
			selectBell.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Analog Clock&quot;</span>;
			selectBell.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">250</span>,selectBell.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">10</span>;
			selectBell.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getBell<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>selectBell<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			selectRadio.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Radio Clock&quot;</span>;
			selectRadio.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">250</span>,selectRadio.<span style="color: #006600;">x</span>=btnWidth+<span style="color: #cc66cc;">10</span>;
			selectRadio.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getRadio<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>selectRadio<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			testAlarm.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Test Alarm&quot;</span>;
			testAlarm.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">250</span>,testAlarm.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">*</span>btnWidth+<span style="color: #cc66cc;">10</span>;
			testAlarm.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,alarmTest<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>testAlarm<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			shutOff.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Turn Off Alarm!&quot;</span>;
			shutOff.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">250</span>,shutOff.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">*</span>btnWidth+<span style="color: #cc66cc;">10</span>;
			shutOff.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,offNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>shutOff<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> getClock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			removeChild<span style="color: #66cc66;">&#40;</span>clockNow<span style="color: #66cc66;">&#41;</span>;
			clockNow=aClock.<span style="color: #006600;">curTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			clockNow.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">20</span>,clockNow.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>clockNow<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> getRadio<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>aClock<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				aClock.<span style="color: #006600;">offAlarm</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			aClock=<span style="color: #000000; font-weight: bold;">new</span> RadioClock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			getClock<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> getBell<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>aClock<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				aClock.<span style="color: #006600;">offAlarm</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			aClock=<span style="color: #000000; font-weight: bold;">new</span> BellClock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			getClock<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> alarmTest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>aClock<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				aClock.<span style="color: #006600;">doAlarm</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>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> offNow<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>aClock<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				aClock.<span style="color: #006600;">offAlarm</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>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As noted, the clocks and their alarms are set up in what might be called &#8220;frozen&#8221; demo mode. In fact, the Client just uses an IAlarmClock implementation and its methods to select a clock and turn the alarm on and off.</p>
<p>We always welcome your comments, and we hope to hear from you about this pattern. Also, we&#8217;re really hoping that you send in some components that you have created that we can put into this app. In the next post, you&#8217;ll be treated to a discussion (rant?) about using parameters in applications like this one. (Duck!)</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%2F2011%2F03%2Factionscript-3-0-saturated-bridge-design-pattern-2-bridge-clock-implementation%2F&amp;title=ActionScript%203.0%20Saturated%20Bridge%20Design%20Pattern%202%3A%20Bridge%20Clock%20Implementation" 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/2011/03/actionscript-3-0-saturated-bridge-design-pattern-2-bridge-clock-implementation/feed/</wfw:commentRss>
		<slash:comments>0</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('p2476code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p247614"><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="p2476code14"><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>
	</channel>
</rss>

