<?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>DaveOnCode &#187; design patterns</title>
	<atom:link href="http://www.daveoncode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>Objective C, iOS and more programming stuff</description>
	<lastBuildDate>Mon, 19 Dec 2011 12:11:12 +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>Fundamental iOS design patterns: SharedInstance (Singleton in Objective C)</title>
		<link>http://www.daveoncode.com/2011/12/19/fundamental-ios-design-patterns-sharedinstance-singleton-objective-c/</link>
		<comments>http://www.daveoncode.com/2011/12/19/fundamental-ios-design-patterns-sharedinstance-singleton-objective-c/#comments</comments>
		<pubDate>Mon, 19 Dec 2011 12:11:12 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[Objective-c and Cocoa]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=875</guid>
		<description><![CDATA[Brief introduction Singleton is one of the (if not THE) most commonly used/abused design pattern adopted in software development. Basically it&#8217;s used to restrict the instantiation of a class to one object only that can be used safely across application architecture. Its implementation is pretty simple compared to other patterns and it&#8217;s very similar for [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2011/12/19/fundamental-ios-design-patterns-sharedinstance-singleton-objective-c/' addthis:title='Fundamental iOS design patterns: SharedInstance (Singleton in Objective C) ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<h2>Brief introduction</h2>
<p>Singleton is one of the (if not THE) most commonly used/abused design pattern adopted in software development.<br />
Basically it&#8217;s used to restrict the instantiation of a class to one object only that can be used safely across application architecture. Its implementation is pretty simple compared to other patterns and it&#8217;s very similar for the most of programming languages, but in Objective C it has a slightly different approach due to the lack of &#8220;normal constructor&#8221; that are instead replaced by alloc/init paradigm.</p>
<h2>How it works in Objective C</h2>
<p>In Objective C, we can speak more appropriately about the SharedInstance pattern rather than traditional Singleton.<br />
This one is often used in Apple frameworks and differently from its twin brother, it does not explicitly forbid the instantiation of multiple class instances, but it simply provide a class method that will return a shared instance. During the first call to that method the instance will be allocated and the next calls will return the previously object created. So this is a sort of hybrid approach.<br />
A common class that use this pattern is <strong>UIApplication</strong> which implements a <strong>sharedApplication</strong> method (which returns the current running application). Differently from Singleton which use <strong>getInstance()</strong>, in Objective C the corresponding method has not a fixed name but it should start with &#8220;<strong>shared</strong>&#8221; followed by the &#8220;type&#8221; of class (so for <strong>UIApplication</strong> is &#8220;<strong>sharedApplication</strong>&#8221; for <strong>MYAmazingManager</strong> will be &#8220;<strong>sharedManager</strong>&#8220;). Anyway there are several exceptions, for example <strong>NSFileManager</strong> has a &#8220;<strong>defaultManager</strong>&#8221; acting as a sharedInstance accessor that could be renamed &#8220;<strong>sharedManager</strong>&#8220;.</p>
<h2>Pattern implementation</h2>
<p>In my singleton implementations I simply call the accessor &#8220;<strong>sharedInstance</strong>&#8220;, in this way I can use a simple pre-compiler macro that automatically synthesize this methods for me.<br />
Even implementation of this pattern may vary and personally I adopted one which is <strong style="text-decoration:underline">thread-safe</strong> and rely on <a href="http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html"><strong>CGD</strong> (<strong>Grand Central Dispatch</strong>)</a>, which is an affective C level API provided by Apple in order to handle concurrency.</p>
<p>The implementation of my sharedInstance method is the following (comments explain how it works):</p>
<div class="codecolorer-container c mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>sharedInstance <br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// structure used to test whether the block has completed or not</span><br />
&nbsp; &nbsp; <span style="color: #993333;">static</span> dispatch_once_t p <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// initialize sharedObject as nil (first call only)</span><br />
&nbsp; &nbsp; __strong <span style="color: #993333;">static</span> id _sharedObject <span style="color: #339933;">=</span> nil<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// executes a block object once and only once for the lifetime of an application</span><br />
&nbsp; &nbsp; dispatch_once<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>p<span style="color: #339933;">,</span> <span style="color: #339933;">^</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _sharedObject <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>self alloc<span style="color: #009900;">&#93;</span> init<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// returns the same object each time</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> _sharedObject<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<h2>Pattern in action!</h2>
<p>SharedInstance pattern is IMO the best way to share data across multiple viewControllers in iOS applications.<br />
One practical example is a data manager to share Core Data stuff such managed object context. I realized my own manager so that I can access this objects anywhere with ease like:</p>
<div class="codecolorer-container c mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>MYDataManager sharedInstance<span style="color: #009900;">&#93;</span> managedObjectContext<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2011/12/19/fundamental-ios-design-patterns-sharedinstance-singleton-objective-c/' addthis:title='Fundamental iOS design patterns: SharedInstance (Singleton in Objective C) ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2011/12/19/fundamental-ios-design-patterns-sharedinstance-singleton-objective-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ArrayCollection: filterFuntion with multiple filter functions using decorator pattern</title>
		<link>http://www.daveoncode.com/2009/03/30/arraycollection-filterfuntion-with-multiple-filter-functions-using-decorator-pattern/</link>
		<comments>http://www.daveoncode.com/2009/03/30/arraycollection-filterfuntion-with-multiple-filter-functions-using-decorator-pattern/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 10:21:11 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[design patterns]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=358</guid>
		<description><![CDATA[I was looking for an elegant solution to implement multiple filter functions on an ArrayCollection (which provides a method filterFunction() that can be assigned dynamically in order to filter data inside the collection). The first and only valid solution I found was that one from Cristian Rotundu, he has extended ArrayCollection class providing a filterFunctions [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/03/30/arraycollection-filterfuntion-with-multiple-filter-functions-using-decorator-pattern/' addthis:title='ArrayCollection: filterFuntion with multiple filter functions using decorator pattern ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I was looking for an elegant solution to implement multiple filter functions on an <strong>ArrayCollection</strong> (which provides a method <strong><em>filterFunction()</em></strong> that can be assigned dynamically in order to filter data inside the collection). The first and only valid solution I found was that one from <a href="http://blog.rotundu.eu/flex/arraycollection-with-multiple-filter-functions/">Cristian Rotundu</a>, he has extended <strong>ArrayCollection</strong> class providing a <strong>filterFunctions</strong> properties which accepts an Array of functions which will be executed in sequence (into a loop) once filterFunction is triggered. Anyway I didn&#8217;t want to subclass <strong>ArrayCollection</strong> and by using the decorator pattern I implemented my own multiple filters version.</p>
<p>So, this is what I done:</p>
<ol>
<li>I defined an interface <strong>IFilter</strong>, which declares only one method: <strong><em>apply()</em></strong></li>
<li>I defined a dummy <strong>Filter</strong> class (which implements <strong>IFilter</strong> in a basic way just to agree to the interface), that is used as a basic empty filter to decorate. It also has a constant called ALL_VALUES which is used as a wildcard for filters (it&#8217;s a simple string with value &#8220;*&#8221;, which means &#8220;all values are allowed&#8221;)</li>
<li>I defined an abstract <strong>AbstractFilterDecorator</strong> class</li>
<li>I created as many subclass of <strong>AbstractFilterDecorator</strong> as the filters I need (yes I treat filters as classes not mere functions)</li>
</ol>
<p>The implementation code is the following:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ArrayCollection = ArrayCollection<span style="color: #66cc66;">&#40;</span>grid.<span style="color: #006600;">dataProvider</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> filter:IFilter = <span style="color: #000000; font-weight: bold;">new</span> Filter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
filter = <span style="color: #000000; font-weight: bold;">new</span> LevelFilter<span style="color: #66cc66;">&#40;</span>filter, levelValue<span style="color: #66cc66;">&#41;</span>;<br />
filter = <span style="color: #000000; font-weight: bold;">new</span> CategoryFilter<span style="color: #66cc66;">&#40;</span>filter, categoryValue<span style="color: #66cc66;">&#41;</span>;<br />
filter = <span style="color: #000000; font-weight: bold;">new</span> DateFilter<span style="color: #66cc66;">&#40;</span>filter, dateValue, <span style="color: #ff0000;">&quot;DD/MM/YY&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">filterFunction</span> = filter.<span style="color: #0066CC;">apply</span>;<br />
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">refresh</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>The filter object is wrapped by decorators which all accepts 2 common arguments: an IFilter reference and an Object representing a value. The DateFilter has a third parameter which is used to configure an internal DateFormatter (from mx.formatters package).<br />
I&#8217;m pretty satisfied :)<br />
Uh&#8230; and if you are worried about performance, it is fast enough because filter object are created once and then the function assigned to <strong>filterFunction</strong> is a reference to the resulting decorated filter.</p>
<p><strong>UPDATE:</strong><br />
I realized a very simple <a href="http://www.daveoncode.com/_dave_stuff/samples/Filters/bin-debug/Filters.html">example application</a> which can be downloaded <a href="http://www.daveoncode.com/_dave_stuff/samples/Filters.zip">here</a>.<br />
This following is the content of the mxml file:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #66cc66;">&gt;</span><br />
<span style="color: #66cc66;">&lt;</span>mx:Application xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #ff0000;">&quot;vertical&quot;</span><span style="color: #66cc66;">&gt;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Script<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">collections</span>.<span style="color: #006600;">ArrayCollection</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">daveoncode</span>.<span style="color: #006600;">filters</span>.<span style="color: #66cc66;">*</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> applyFilters<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ArrayCollection &nbsp;= ArrayCollection<span style="color: #66cc66;">&#40;</span>userGrid.<span style="color: #006600;">dataProvider</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> filter:IFilter = <span style="color: #000000; font-weight: bold;">new</span> Filter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filter = <span style="color: #000000; font-weight: bold;">new</span> AgeFilter<span style="color: #66cc66;">&#40;</span>filter, ageFilterValue.<span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filter = <span style="color: #000000; font-weight: bold;">new</span> DateFilter<span style="color: #66cc66;">&#40;</span>filter, dateFilterValue.<span style="color: #006600;">selectedDate</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filter = <span style="color: #000000; font-weight: bold;">new</span> SexFilter<span style="color: #66cc66;">&#40;</span>filter, sexFilterValue.<span style="color: #006600;">value</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">data</span>.<span style="color: #006600;">filterFunction</span> = filter.<span style="color: #0066CC;">apply</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">data</span>.<span style="color: #006600;">refresh</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:Script<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Panel title=<span style="color: #ff0000;">&quot;Filters&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;600&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Form<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:FormItem label=<span style="color: #ff0000;">&quot;Age:&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:TextInput id=<span style="color: #ff0000;">&quot;ageFilterValue&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:FormItem<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:FormItem label=<span style="color: #ff0000;">&quot;Sex:&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:ComboBox id=<span style="color: #ff0000;">&quot;sexFilterValue&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{[{data: Filter.ALL_VALUES, label: 'ALL'}, {data: 'm', label: 'Male'}, {data: 'f', label: 'Female'}]}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:FormItem<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:FormItem label=<span style="color: #ff0000;">&quot;Join date:&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:DateField id=<span style="color: #ff0000;">&quot;dateFilterValue&quot;</span> formatString=<span style="color: #ff0000;">&quot;DD/MM/YY&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:FormItem<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:FormItem<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span> label=<span style="color: #ff0000;">&quot;Apply filters&quot;</span> click=<span style="color: #ff0000;">&quot;{this.applyFilters()}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:FormItem<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:Form<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:Panel<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Panel title=<span style="color: #ff0000;">&quot;Users&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;600&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:DataGrid id=<span style="color: #ff0000;">&quot;userGrid&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:dataProvider<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:ArrayCollection<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Array<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;24&quot;</span> sex=<span style="color: #ff0000;">&quot;f&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Susan&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2007, 5, 15)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;36&quot;</span> sex=<span style="color: #ff0000;">&quot;f&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Ashley&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(1998, 7, 20)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;24&quot;</span> sex=<span style="color: #ff0000;">&quot;f&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Jennifer&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2001, 3, 24)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;19&quot;</span> sex=<span style="color: #ff0000;">&quot;f&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Emma&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2002, 3, 24)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;44&quot;</span> sex=<span style="color: #ff0000;">&quot;f&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Carol&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(1999, 9, 16)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;28&quot;</span> sex=<span style="color: #ff0000;">&quot;m&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Peter&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2005, 3, 12)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;35&quot;</span> sex=<span style="color: #ff0000;">&quot;m&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Mike&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2008, 10, 10)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;26&quot;</span> sex=<span style="color: #ff0000;">&quot;m&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Dave&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2008, 10, 10)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;44&quot;</span> sex=<span style="color: #ff0000;">&quot;m&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;William&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2004, 9, 16)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;24&quot;</span> sex=<span style="color: #ff0000;">&quot;m&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Sean&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2006, 3, 24)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:Array<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:ArrayCollection<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:dataProvider<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:DataGrid<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:Panel<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&lt;/</span>mx:Application<span style="color: #66cc66;">&gt;</span></div></td></tr></tbody></table></div>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/03/30/arraycollection-filterfuntion-with-multiple-filter-functions-using-decorator-pattern/' addthis:title='ArrayCollection: filterFuntion with multiple filter functions using decorator pattern ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/03/30/arraycollection-filterfuntion-with-multiple-filter-functions-using-decorator-pattern/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Learning design patterns with Actionscript 3: episode 2 &#8211; Decorator pattern</title>
		<link>http://www.daveoncode.com/2009/03/30/learning-design-patterns-with-actionscript-3-episode-2-decorator-pattern/</link>
		<comments>http://www.daveoncode.com/2009/03/30/learning-design-patterns-with-actionscript-3-episode-2-decorator-pattern/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 09:21:26 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[design patterns]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=354</guid>
		<description><![CDATA[The objective of the decorator pattern is to provide a way to add extra features and responsibilities to a class at runtime without to subclass it. The classes diagram of this pattern is composed by an interface or an abstract class which will define the supertype of the class that will be extended (decorated) by [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/03/30/learning-design-patterns-with-actionscript-3-episode-2-decorator-pattern/' addthis:title='Learning design patterns with Actionscript 3: episode 2 &#8211; Decorator pattern ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>The objective of the decorator pattern is to provide a way to add extra features and responsibilities to a class at runtime without to subclass it.</p>
<p>The classes diagram of this pattern is composed by an interface or an abstract class which will define the supertype of the class that will be extended (decorated) by one or more classes implementing the same interface and extending an abstract class which identify these as decorator classes.</p>
<p><span id="more-354"></span></p>
<p>A real scenario into which apply this pattern can be a cars store. A car is the base class and the accessories such dvd player, gps, air conditioning and so on, can be thought as decorators. Once decorated, the car (which has a method getPrice() which returns its cost) will get updated by accessories, so calling the getPrice() method will include the price of the “naked” car plus the price of the various accessories installed.</p>
<p>Let’s take a look to the classes…</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.<span style="color: #006600;">mycarstore</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IVehicle <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>The interface above will be implemented by all the classes and we will treat the car as an IVehicle not a concrete Car class.<br />
This is our simple Car class:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.<span style="color: #006600;">mycarstore</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Car <span style="color: #0066CC;">implements</span> IVehicle <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Car<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">50000</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;Base car&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now is time to see the decorators (in our scenario the accessories). This is the abstract Accessory class, all accessories will extend it:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.<span style="color: #006600;">mycarstore</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Accessory <span style="color: #0066CC;">implements</span> IVehicle <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; protected <span style="color: #000000; font-weight: bold;">var</span> _vehicle:IVehicle;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Accessory<span style="color: #66cc66;">&#40;</span>vehicle:IVehicle<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>._vehicle = vehicle;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._vehicle.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._vehicle.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>In Accessory we can notice that the class encapsulate an object of IVehicle which is passed to the constructor (of course since this class is abstract the object will be passed to the constructor of Accessory&#8217;s subclasses). This is the key concept of decorator pattern, all decorators keep a reference of the interface they are going to decorate and this reference is used as the base for methods business logic. Let&#8217;s see how:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.<span style="color: #006600;">mycarstore</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DVDPlayer <span style="color: #0066CC;">extends</span> Accessory <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DVDPlayer<span style="color: #66cc66;">&#40;</span>vehicle:IVehicle<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>vehicle<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._vehicle.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">550</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._vehicle.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;, DVD Player&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>DVDPlayer is a concrete decorator and it uses the encapsulated IVehicle object in order to provide an extended version of the interface methods (getPrice() and getDescription()). Basically it uses first the reference methods and then it adds some other values.</p>
<p>To use the pattern we will first instantiate a Car object, than we will wrap it with one or more decorators:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> car:IVehicle = <span style="color: #000000; font-weight: bold;">new</span> Car<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Price: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; - description: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
car = <span style="color: #000000; font-weight: bold;">new</span> DVDPlayer<span style="color: #66cc66;">&#40;</span>car<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Price: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; - description: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
car = <span style="color: #000000; font-weight: bold;">new</span> GPSSystem<span style="color: #66cc66;">&#40;</span>car<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Price: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; - description: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>The output will be:</p>
<pre>
Price: 50000 - description: Base car
Price: 50550 - description: Base car, DVD Player
Price: 50875 - description: Base car, DVD Player, GPS System
</pre>
<p>The order into which we wrap the concrete component (Car) is indifferent (we can add GPSSystem before or after DVDPlayer) and of course despite its readability, we can wrap the Car in a single statement:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> car2:IVehicle = <span style="color: #000000; font-weight: bold;">new</span> GPSSystem<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DVDPlayer<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Car<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>In this example I used very simple classes, however in a real implementation we can create more sophisticated decorators which will accept extra arguments beyond the IVehicle reference.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/03/30/learning-design-patterns-with-actionscript-3-episode-2-decorator-pattern/' addthis:title='Learning design patterns with Actionscript 3: episode 2 &#8211; Decorator pattern ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/03/30/learning-design-patterns-with-actionscript-3-episode-2-decorator-pattern/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Learning design patterns with Actionscript 3: episode 1 &#8211; Singleton pattern</title>
		<link>http://www.daveoncode.com/2008/12/28/learning-design-patterns-with-actionscript-3-episode-1-singleton-pattern/</link>
		<comments>http://www.daveoncode.com/2008/12/28/learning-design-patterns-with-actionscript-3-episode-1-singleton-pattern/#comments</comments>
		<pubDate>Sun, 28 Dec 2008 15:22:47 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://daveoncode.wordpress.com/?p=169</guid>
		<description><![CDATA[In these Christmas holidays I bought some new books, among them I purchased &#8220;Actionscript 3 design patterns&#8221; (written by William Sanders and Chandima Cumaranatunge) and I&#8217;m actually studying this one. I never faced before the world of design patterns (which is, for those that never heard about, a way to write code and solve problems [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2008/12/28/learning-design-patterns-with-actionscript-3-episode-1-singleton-pattern/' addthis:title='Learning design patterns with Actionscript 3: episode 1 &#8211; Singleton pattern ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>In these Christmas holidays I bought some new books, among them I purchased &#8220;Actionscript 3 design patterns&#8221; (written by William Sanders and Chandima Cumaranatunge) and I&#8217;m actually studying this one. I never faced before the world of design patterns (which is, for those that never heard about, a way to write code and solve problems by implementing tested strategies that will guarantee flexibility and extensibility), or better I knew what a design pattern is, but I&#8217;m trying to learn to use them for the first time.<br />
Unfortunately the first thing I learned about implementing design patterns in Actionscript 3 is that for some stuff we have to use workarounds, because actually as3 doesn&#8217;t provides 2 little useful things: <strong><em>abstract classes</em></strong> (on which most of the patterns are based) and not public constructor (in Java we can declare a class constructor as <strong><em>private</em></strong>, in as3 we must declare it as <strong><em>public</em></strong>), anyway with some little efforts, we can do everything :-)</p>
<p><span id="more-169"></span>Another thing I learned is that design patterns provide a way to solve problems and simplify application&#8217;s modification/extension but each pattern can be implemented in a little different way from a language to another and from a developer to another, the only thing that must be unchanged is the goal of the pattern and how it is used.<br />
One, or maybe the simplest, pattern around is the <em><strong>Singleton</strong></em> pattern, which is even one of the most used, even without be aware of it, we used something similar. In fact, Singleton pattern is used when a class must be instantiated only one time in the application (for example a shopping cart in an e-commerce website) or better when a class must exists as only one and be accessible everywhere through a &#8220;common access point&#8221; which is for convenience a static method called <strong><em>getInstance()</em></strong>.</p>
<p>With Java a singleton implementation should be something like this:</p>
<pre>public class ClassicSingleton {

   private static ClassicSingleton instance = null;

   protected ClassicSingleton() {

   }

   public static ClassicSingleton getInstance() {

      if(instance == null) {
         instance = new ClassicSingleton();
      }

      return instance;

   }

}</pre>
<p>(I taken the code above from this article on javaworld.com: http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html)</p>
<p>In Actionscript 3, the best implementation (in my opinion) of a Singleton pattern is the following:</p>
<pre>package {

    import flash.errors.IllegalOperationError;

    public class SingletonClass {

        private static var instance:SingletonClass = new SingletonClass();

        public function SingletonClass() {

            if (instance != null) {

                throw new IllegalOperationError("SingletonClass can't be instantiated because is a Singleton. Use getInstance() to retrieve a class reference.");

            }

        }

        public static function getInstance():SingletonClass {

            return instance;

        }

    }

}</pre>
<p>In this way if we try to call the SingletonClass constructor by using the new operator:</p>
<pre>var mySingletonClass:SingletonClass = new SingletonClass();</pre>
<p>we will get the error: &#8220;SingletonClass can&#8217;t be instantiated because is a Singleton. Use getInstance() to retrieve a class reference.&#8221;. So, the only and sound way to retrieve our SingletonClass is to use <strong><em>getInstance()</em></strong>:</p>
<pre>var mySingletonClass:SingletonClass = SingletonClass.getInstance();</pre>
<p>Every time we will use <strong><em>getInstance()</em></strong> the same class reference will be returned, avoiding multiple class instantiations.</p>
<p>This is clear and good, but initially I was wondering why to use a method to retrieve the instance, in my own custom implementation I wrote the following code:</p>
<pre>package {

    import flash.errors.IllegalOperationError;

    public class AbnormalSingleton {

        private static var created:Boolean;

        public function AbnormalSingleton()    {

            if (!created) {

                created = true;

            } else {

                throw new IllegalOperationError("AbnormalSingleton can't be instantiated again because is a Singleton.");

            }

        }

    }

}</pre>
<p>With my AbnormalSingleton the first goal of the Singleton pattern is reached (you can&#8217;t get more than one instance), but the pattern has also a second goal: provide a common access point to the reference, and without the getInstance() method, this goal can&#8217;t be accomplished, moreover patterns are used to avoid errors and to be used by several developers in a clear manner. So, my idea was not so smart, because you can use the new operator to instantiate the class for the first time with no errors, but the second time you will get an error, the situation is not clear! Instead with the previous example, is immediately evident that we can&#8217;t use the class to generate multiple instance.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2008/12/28/learning-design-patterns-with-actionscript-3-episode-1-singleton-pattern/' addthis:title='Learning design patterns with Actionscript 3: episode 1 &#8211; Singleton pattern ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2008/12/28/learning-design-patterns-with-actionscript-3-episode-1-singleton-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

