<?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>Geirr Winnem&#039;s Blog &#187; Design Patterns</title>
	<atom:link href="http://geirr.winnem.com/blog/category/actionscript/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://geirr.winnem.com/blog</link>
	<description>Ranting about java-net-actionscript</description>
	<lastBuildDate>Thu, 09 Sep 2010 21:38:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Factory method design pattern as3 example</title>
		<link>http://geirr.winnem.com/blog/2009/12/30/factory-method-design-pattern-as3-example/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://geirr.winnem.com/blog/2009/12/30/factory-method-design-pattern-as3-example/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 11:40:54 +0000</pubDate>
		<dc:creator>gwinnem</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[abstract class]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[factory method]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[software design]]></category>

		<guid isPermaLink="false">http://geirr.winnem.com/blog/?p=403</guid>
		<description><![CDATA[The Factory method pattern belongs to the group of creational patterns,
	it deals with the problem of creating objects (products) without specifying the exact&#160;class of object that will be created.
	
	
The factory method design pattern handles this problem by defining a separate method for creating the objects.
Subclasses can then override to specify the derived type of product [...]]]></description>
			<content:encoded><![CDATA[<div><span style="font-family: trebuchet ms,helvetica,sans-serif;"><span style="font-size: 14px;">The <b>Factory method pattern </b>belongs to the group of <em>creational patterns</em>,<br />
	it deals with the problem of creating objects (products) without specifying the exact&nbsp;class of object that will be created.<br />
	</span><br />
	</span></div>
<div><span style="font-family: trebuchet ms,helvetica,sans-serif;"><span style="font-size: 14px;">The factory method design pattern handles this problem by defining a separate method for creating the objects.</span></span></div>
<div><span style="font-family: trebuchet ms,helvetica,sans-serif;"><span style="font-size: 14px;">Subclasses can then override to specify the derived type of product that will be created. <br />
	More generally,the term <i>factory method</i> is often used to refer to any method whose main purpose is creation of objects</span></span>.</div>
<div><span id="more-403"></span></div>
<p><span style="font-size: 14px;"><span style="font-family: trebuchet ms,helvetica,sans-serif;"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span class="Apple-style-span" style="line-height: 15px; text-align: left;">The Factory Method pattern is a simplified version of Abstract Factory pattern. </span></span><br />
	</span></span></p>
<p><span style="font-size: 14px;"><span style="font-family: trebuchet ms,helvetica,sans-serif;"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span class="Apple-style-span" style="line-height: 15px; text-align: left;">Factory Method pattern is responsible of creating products that belong to one family, while Abstract Factory pattern deals with multiple families of products.</span></span></span></span></p>
<p><strong>Factory method <a href="http://en.wikipedia.org/wiki/Unified_Modeling_Language">UML</a> diagram</strong></p>
<p><a href="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/FactoryMethod.gif#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img alt="FactoryMethod" class="aligncenter size-full wp-image-482" height="301" src="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/FactoryMethod.gif" title="FactoryMethod" width="546" /></a><a href="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/FactoryMethod.gif#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"> </a></p>
<p><strong>Participants:<br />
	</strong></p>
<ul>
<li><strong>IProduct</strong> Defines the interface of the objects the factory method creates.</li>
<li><strong>ConcreteProduct1</strong> Implements the IProduct interface.</li>
<li><strong>ConcreteProduct2</strong> Implements the IProduct interface.</li>
<li><strong>AbstractCreator</strong> Declares the factory method, which returns an object of type IProduct. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.</li>
<li><strong>ConcreteCreator1</strong> Overrides the factoryMethod to return an instance of ConcreteProduct1.</li>
<li><strong>ConcreteCreator2</strong> Overrides the factoryMethod to return an instance of ConcreteProduct2.</li>
</ul>
<p>For more details about how an abstract class is defined in actionscript, see <a href="http://geirr.winnem.com/blog/design-patterns/abstract-class-example/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed">AbstractClass</a></p>
<p><strong>Benefits:<br />
	</strong></p>
<ul>
<li>Eliminates the needs to bind application classes into your code. The code deals with interfaces so it can work with any classes that implement a certain interface.</li>
<li>Enables the subclasses to provide an extended version of an object, because creating an object inside a class is more flexible than creating an object directly in the client.</li>
</ul>
<p><strong>Usage:<br />
	</strong></p>
<ul>
<li>When a class cannot anticipate the objects it must create.</li>
<li>When a class want subclasses to specify the objects it creates.</li>
<li>Classes delegate responsability to one of several helper subclasses, and you want to localize the knowledge of which helper subclasses is the delegate.</li>
</ul>
<p>&nbsp;</p>
<p><strong>Live example:<br />
	</strong></p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" height="300" width="400"><param name="quality" value="high" /><param name="movie" value="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/DesignPatternFactoryMethod.swf" /><embed height="300" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" src="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/DesignPatternFactoryMethod.swf" type="application/x-shockwave-flash" width="400"></embed></object></p>
<p>&nbsp;</p>
<p><strong><span style="font-size: 16px;">Source code: </span></strong></p>
<p><a href="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/DesignPatternFactoryMethod.zip#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed">DesignPatternFactoryMethod</a></p>
]]></content:encoded>
			<wfw:commentRss>http://geirr.winnem.com/blog/2009/12/30/factory-method-design-pattern-as3-example/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>#Abstract class example in #as3(#actionscript)</title>
		<link>http://geirr.winnem.com/blog/2009/12/30/abstract-class-example-in-as3actionscript/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://geirr.winnem.com/blog/2009/12/30/abstract-class-example-in-as3actionscript/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 23:17:19 +0000</pubDate>
		<dc:creator>gwinnem</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://geirr.winnem.com/blog/?p=473</guid>
		<description><![CDATA[<p>Some of the design patterns rely on the implementation of abstract classes. Actionscript unfortunately do not support this.</p>
<p>Some facts about abstract classes:</p>

Abstract classes cannot be instantiated. They have to be extended by subclasses.
They can contain abstract methods or unimplemented method declarations that subclasses need to implement. The methods implemented in an abstract class will in [...]]]></description>
			<content:encoded><![CDATA[<p>Some of the design patterns rely on the implementation of abstract classes. Actionscript unfortunately do not support this.</p>
<p>Some facts about abstract classes:</p>
<ul>
<li>Abstract classes cannot be instantiated. They have to be extended by subclasses.</li>
<li>They can contain abstract methods or unimplemented method declarations that subclasses need to implement. The methods implemented in an abstract class will in most cases be default behaviors, and much of the class will be unimplemented.</li>
<li>Before a class derived from an abstract class can be instantiated, it must implement all unimplemented methods. The advantage of deriving from an abstract class is that the subclass does not have to implement a method if the default behavior implemented in the abstract class is what it needs.</li>
</ul>
<p>&nbsp;</p>
<p>Its quite easy to mimic this in actionscript, but there will be no compiler error checking, so its up to the developer to make sure that it is being implemented correctly.</p>
<p><strong>Sample Abstract Class:<br />
	</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">package winnemconsulting.<span class="me1">examples</span>.<span class="me1">abstract</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">errors</span>.<span class="me1">IllegalOperationError</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">utils</span>.<span class="me1">getQualifiedClassName</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Simple example of a abstract class implementation</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @author Geirr Winnem</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> AbstractClass</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Constructor.</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Since this is a abstract class it is not allowed to instanciate this class.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> AbstractClass<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Checking if this class is being instanciated directly</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>getQualifiedClassName<span class="br0">&#40;</span><span class="kw3">this</span><span class="br0">&#41;</span> == <span class="st0">&quot;winnemconsulting.examples.abstract::AbstractClass&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">throw</span> <span class="kw2">new</span> IllegalOperationError<span class="br0">&#40;</span><span class="st0">&quot;Abstract class can not be instanciated&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Normal function which is implemented in the abstract class</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> implementedFunction<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="st0">&quot;implementedFunction&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Abstract function which must be overriden in all classes derived from this class</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> abstractFunction<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">throw</span> <span class="kw2">new</span> IllegalOperationError<span class="br0">&#40;</span><span class="st0">&quot;Abstract function must be ovverriden in subclass&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://geirr.winnem.com/blog/2009/12/30/abstract-class-example-in-as3actionscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design patterns tutorial(#as3 #actionscript) overview</title>
		<link>http://geirr.winnem.com/blog/2009/12/16/design-patterns-tutorialas3-actionscript-overview/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://geirr.winnem.com/blog/2009/12/16/design-patterns-tutorialas3-actionscript-overview/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 10:05:29 +0000</pubDate>
		<dc:creator>gwinnem</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[software design]]></category>

		<guid isPermaLink="false">http://geirr.winnem.com/blog/?p=401</guid>
		<description><![CDATA[<p>Ive been working with design patterns for a long time, and have had some really good experiences in the past when im refactoring code and enforcing the use of patterns.</p>
<p>In the following tutorials i plan to give brief explanations on each pattern, how to use it and example code where the pattern is being used.</p>
<p>Im [...]]]></description>
			<content:encoded><![CDATA[<p>Ive been working with design patterns for a long time, and have had some really good experiences in the past when im refactoring code and enforcing the use of patterns.</p>
<p>In the following tutorials i plan to give brief explanations on each pattern, how to use it and example code where the pattern is being used.</p>
<p>Im not going to divide the patterns into the normal categories(Creational,Behavioral or Structural).</p>
<p>Rather im planning to present the patterns in a way which gives the audience a progressive understanding on patterns and their interlinking between each other.</p>
<p>&nbsp;</p>
<p><strong>Pattern list for these tutorials:</strong></p>
<ol>
<li>Factory Method</li>
<li>Strategy</li>
<li>Decorator</li>
<li>Composite</li>
<li>Iterator</li>
<li>Template Method</li>
<li>Abstract Factory</li>
<li>Builder</li>
<li>Singleton</li>
<li>Proxy</li>
<li>Adapter</li>
<li>Bridge</li>
<li>Mediator</li>
<li>Observer</li>
<li>Chain of Responsibillity</li>
<li>Memento</li>
<li>Command</li>
<li>Prototype</li>
<li>State</li>
<li>Visitor</li>
<li>Flyweight</li>
<li>Interpreter</li>
<li>Facade</li>
</ol>
<p>&nbsp;</p>
<p>So stay tuned for the first part where i will walk through the Factory method pattern.</p>
<p>&nbsp;</p>
<p>Please feel free to comment and share thoughts as these tutorials are progressing.</p>
]]></content:encoded>
			<wfw:commentRss>http://geirr.winnem.com/blog/2009/12/16/design-patterns-tutorialas3-actionscript-overview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>#Actionscript(#as3) #Command #pattern with source code</title>
		<link>http://geirr.winnem.com/blog/2009/12/14/command-design-pattern-in-actionscriptas3-with-source-code/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://geirr.winnem.com/blog/2009/12/14/command-design-pattern-in-actionscriptas3-with-source-code/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 10:47:20 +0000</pubDate>
		<dc:creator>gwinnem</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[command pattern]]></category>
		<category><![CDATA[design pattern]]></category>

		<guid isPermaLink="false">http://geirr.winnem.com/blog/?p=379</guid>
		<description><![CDATA[<p>The&#160;command pattern&#160;is a&#160;design pattern&#160;in which an object is used to represent and&#160;encapsulate&#160;all the information needed to call a method at a later time.&#160;</p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em; ">This information includes the method name, the object that owns the method and values for the method parameters.</p>
<p style="margin-top: 0.4em; margin-right: 0px; [...]]]></description>
			<content:encoded><![CDATA[<p><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: sans-serif; font-size: 13px; line-height: 19px; ">The&nbsp;<b>command pattern</b>&nbsp;is a&nbsp;<a href="http://en.wikipedia.org/wiki/Design_pattern_(computer_science)" style="text-decoration: none; color: rgb(90, 54, 150); background-image: none; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: initial; background-position: initial initial; " title="Design pattern (computer science)">design pattern</a>&nbsp;in which an object is used to represent and&nbsp;<a class="mw-redirect" href="http://en.wikipedia.org/wiki/Information_Hiding" style="text-decoration: none; color: rgb(0, 43, 184); background-image: none; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: initial; background-position: initial initial; " title="Information Hiding">encapsulate</a>&nbsp;all the information needed to call a method at a later time.&nbsp;</span></p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em; "><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: sans-serif; font-size: 13px; line-height: 19px; ">This information includes the method name, the object that owns the method and values for the method parameters.</span></p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em; "><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: sans-serif; font-size: 13px; line-height: 19px; ">Three terms always associated with the command pattern are&nbsp;<i>client</i>,&nbsp;<i>invoker</i>&nbsp;and&nbsp;<i>receiver</i>. <br />
	</span></p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em; "><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: sans-serif; font-size: 13px; line-height: 19px; ">The&nbsp;<i>client</i>&nbsp;instantiates the command object and provides the information required to call the method at a later time. The&nbsp;<i>invoker</i>&nbsp;decides when the method should be called. The&nbsp;<i>receiver</i>&nbsp;is an instance of the class that contains the method&#39;s code.</span></p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em; "><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: sans-serif; font-size: 13px; line-height: 19px; ">Using command objects makes it easier to construct general comp<span class="Apple-style-span" style="font-family: Arial, Verdana, sans-serif; font-size: 12px; line-height: normal; color: rgb(34, 34, 34); "><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: sans-serif; font-size: 13px; line-height: 19px; ">onents that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.</span></span></span></p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em; "><span id="more-379"></span></p>
<p>The example app uses one single command class to generate 3 canvas DisplayObjects to the stage.</p>
<p>&nbsp;</p>
<p>In the DesignPatternCommand.mxml file im using the CreationComplete event to kick of the Command sequence.</p>
<p>&nbsp;</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">private</span> <span class="kw2">function</span> onCreationComplete<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> commandCanvas:Command=<span class="kw2">new</span> Command<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; commandCanvas.<span class="me1">execute</span><span class="br0">&#40;</span>Command.<span class="me1">RED</span>, <span class="kw3">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; commandCanvas=<span class="kw2">new</span> Command<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; commandCanvas.<span class="me1">execute</span><span class="br0">&#40;</span>Command.<span class="me1">GREEN</span>, <span class="kw3">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; commandCanvas=<span class="kw2">new</span> Command<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; commandCanvas.<span class="me1">execute</span><span class="br0">&#40;</span>Command.<span class="me1">BLUE</span>, <span class="kw3">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>Since its good practice in OOP i also created a interface to program the Command class.</p>
<p>ICommand.as</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="me1">DisplayObjectContainer</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* The interface of command implementation</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp;* @author Geirr Winnem</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw3">interface</span> ICommand</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Execute the &lt;code&gt;ICommand&lt;/code&gt; logic.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param canvasColor the canvas color to generate</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param stage DisplayObjectContainer which the canvas is added to.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">function</span> execute<span class="br0">&#40;</span>canvasColor:<span class="kw3">String</span>, <span class="kw3">stage</span>: DisplayObjectContainer<span class="br0">&#41;</span>:<span class="kw3">void</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>&nbsp;</p>
<p>Finally we have the command class itself which implements the ICommand interface</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="me1">DisplayObjectContainer</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">import</span> mx.<span class="me1">containers</span>.<span class="me1">Canvas</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Class implementing the Command pattern</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* @author Geirr Winnem</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">class</span> Command <span class="kw3">extends</span> Canvas <span class="kw3">implements</span> ICommand</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Constant value for a red canvas</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw3">static</span> const RED:<span class="kw3">String</span>=<span class="st0">&quot;redCanvas&quot;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Constant value for a green canvas</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw3">static</span> const GREEN:<span class="kw3">String</span>=<span class="st0">&quot;greenCanvas&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Constant value for a blue canvas</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw3">static</span> const BLUE:<span class="kw3">String</span>=<span class="st0">&quot;blueCanvas&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Constructor</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> Command<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">super</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Execute the &lt;code&gt;ICommand&lt;/code&gt; logic.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param canvasColor the canvas color to generate</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param stage DisplayObjectContainer which the canvas is added to.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> execute<span class="br0">&#40;</span>canvasColor:<span class="kw3">String</span>, mainStage:DisplayObjectContainer<span class="br0">&#41;</span>:<span class="kw3">void</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">switch</span> <span class="br0">&#40;</span>canvasColor<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> RED:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">super</span>.<span class="kw3">setStyle</span><span class="br0">&#40;</span><span class="st0">&quot;backgroundColor&quot;</span>, 0xff0000<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> GREEN:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">super</span>.<span class="kw3">setStyle</span><span class="br0">&#40;</span><span class="st0">&quot;backgroundColor&quot;</span>, 0&#215;00ff00<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> BLUE:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">super</span>.<span class="kw3">setStyle</span><span class="br0">&#40;</span><span class="st0">&quot;backgroundColor&quot;</span>, 0&#215;0000ff<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="kw3">width</span>=<span class="nu0">100</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="kw3">height</span>=<span class="nu0">100</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mainStage.<span class="me1">addChild</span><span class="br0">&#40;</span><span class="kw3">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>&nbsp;</p>
<p style="text-align: center;"><strong>Example App..<br />
	</strong></p>
<p style="text-align: center; "><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" height="300" width="300"><param name="quality" value="high" /><param name="wmode" value="window" /><param name="allowScriptAccess" value="samedomain" /><param name="scale" value="exactfit" /><param name="movie" value="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/DesignPatternCommand.swf" /><embed allowscriptaccess="samedomain" height="300" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" scale="exactfit" src="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/DesignPatternCommand.swf" type="application/x-shockwave-flash" width="300" wmode="window"></embed></object></p>
<p style="text-align: center; ">&nbsp;</p>
<p><a href="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/DesignPatternCommand.zip#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed">DesignPatternCommand flex project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://geirr.winnem.com/blog/2009/12/14/command-design-pattern-in-actionscriptas3-with-source-code/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>#Actionscript(#as3) #Facade #Pattern</title>
		<link>http://geirr.winnem.com/blog/2009/12/07/actionscriptas3-facade-pattern/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://geirr.winnem.com/blog/2009/12/07/actionscriptas3-facade-pattern/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 16:58:15 +0000</pubDate>
		<dc:creator>gwinnem</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[facade]]></category>

		<guid isPermaLink="false">http://geirr.winnem.com/blog/?p=340</guid>
		<description><![CDATA[<p>&#160;</p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em; ">A facade is an object that provides a simplified interface to a larger body of code, such as a&#160;class library.</p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em; ">A facade can:</p>

make a software library&#160;easier to use and understand, since the facade has [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em; ">A facade is an object that provides a simplified interface to a larger body of code, such as a&nbsp;class library.</p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em; ">A facade can:</p>
<ul style="line-height: 1.5em; list-style-type: square; margin-top: 0.3em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 1.5em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-image: url(http://en.wikipedia.org/skins-1.5/monobook/bullet.gif); ">
<li>make a software library&nbsp;easier to use and understand, since the facade has convenient methods for common tasks;</li>
<li>make code that uses the library more readable, for the same reason;</li>
<li>reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;</li>
<li>wrap a poorly-designed collection of APIs with a single well-designed API (as per task needs).</li>
</ul>
<p><span id="more-340"></span></p>
<p style="text-align: center; "><a href="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/FacadeDesignPattern.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"> <img alt="Facade design pattern" class="size-full wp-image-341" height="388" src="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/FacadeDesignPattern.png" title="FacadeDesignPattern" width="511" /> </a></p>
<p style="text-align: center; ">&nbsp;</p>
<p>You read more about design patterns at&nbsp;<a href="http://www.as3dp.com/category/design-patterns/">http://www.as3dp.com/category/design-patterns/</a></p>
<p>&nbsp;</p>
<p>You can find the source code here:&nbsp;<a href="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/designpatternfacade.zip#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed">DesignpatternFacade.zip</a>&nbsp;flex project.</p>
]]></content:encoded>
			<wfw:commentRss>http://geirr.winnem.com/blog/2009/12/07/actionscriptas3-facade-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>#Actionscript(#as3) #Observer #pattern</title>
		<link>http://geirr.winnem.com/blog/2009/12/02/observer-design-pattern-in-actionscript/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://geirr.winnem.com/blog/2009/12/02/observer-design-pattern-in-actionscript/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 15:55:57 +0000</pubDate>
		<dc:creator>gwinnem</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[observer]]></category>
		<category><![CDATA[pattern]]></category>

		<guid isPermaLink="false">http://geirr.winnem.com/blog/?p=277</guid>
		<description><![CDATA[<p>Any good software developer should know about design patterns. I have been using them since i started programming. Now im doing most of my work in java and actionscript and in one of my opensource projects i had to avoid using the actionscript event model.</p>
<p>The Observer pattern is a subset of the&#160;Publish/subscribe&#160;pattern. and belongs to [...]]]></description>
			<content:encoded><![CDATA[<p>Any good software developer should know about design patterns. I have been using them since i started programming. Now im doing most of my work in java and actionscript and in one of my opensource projects i had to avoid using the actionscript event model.</p>
<p>The Observer pattern is a subset of the&nbsp;<a href="http://en.wikipedia.org/wiki/Publish/subscribe">Publish/subscribe</a>&nbsp;pattern. and belongs to the Behavioral group of patterns.</p>
<p><span id="more-277"></span></p>
<p>Basically when implementing this pattern you have two actors:</p>
<ul>
<li><code>Producer/Publisher</code></li>
<li><code>Subscriber/Consumer</code></li>
</ul>
<p>The Producer/Publisher is responsible for broadcasting the message to all Subscriber/Consumer objects that subscribe/consume for a specific message/event.</p>
<p>In the actionscript event model this is handled by adding a eventlistener of a specific type(Subscriber/Consumer) and by dispacthing an event(Producer/Publisher)</p>
<p><strong>Psoudo code Producer/Publisher:</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">events</span>.<span class="me1">Event</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">events</span>.<span class="me1">EventDispatcher</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> Producer <span class="kw3">extends</span> EventDispatcher</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/** </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Empty contructor</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> Producer<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Event dispatcher.</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Responsible for notifying all the subscribers </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param eventType</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> notifyConsumer<span class="br0">&#40;</span>eventType:<span class="kw3">String</span><span class="br0">&#41;</span>:<span class="kw3">void</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dispatchEvent<span class="br0">&#40;</span><span class="kw2">new</span> Event<span class="br0">&#40;</span>eventType<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p><strong>Psoudo code Subscriber/Consumer:</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">events</span>.<span class="me1">Event</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> Consumer</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">var</span> producer:Producer;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Constructor</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Adding the eventlistener </span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> Consumer<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; producer.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="st0">&quot;MyListenerEvent&quot;</span>, onEvent<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Handling the event </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param event Event object dispatched from producer</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @private</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> onEvent<span class="br0">&#40;</span>event:Event=<span class="kw2">null</span><span class="br0">&#41;</span>:<span class="kw3">void</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Handle the event here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>The above code is a example.</p>
<p><strong>Observer Class Diagram:</strong></p>
<p><img alt="observer" class="aligncenter size-full wp-image-278" height="308" src="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/observer.gif" title="observer" width="414" /></p>
<p><strong>Observer Sequence Diagram:</strong></p>
<p><img alt="observersequence" class="alignnone size-full wp-image-279" height="298" src="http://geirr.winnem.com/blog/wp-content/uploads/2009/12/observersequence.gif" title="observersequence" width="616" /></p>
<p>&nbsp;</p>
<p><strong>References:</strong></p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Observer_pattern">Observer_pattern at Wikipedia<br />
		</a></li>
<li><a href="http://en.wikipedia.org/wiki/Design_pattern_(computer_science)">Software design patterns at Wikipedia<br />
		</a></li>
<li><a href="http://www.research.ibm.com/designpatterns/example.htm">Definition from IBM<br />
		</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://geirr.winnem.com/blog/2009/12/02/observer-design-pattern-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
